staticメソッド
効果:メソッド間を超えて変数を使えるようになる。
ルール①:使用したい変数の頭に static を付ける。
例:
private **static** int count = 0;
ルール②:ルール①で設定した変数を出力するメソッドの頭に static を付ける。
例:
public **static** void display(){
System.out.println(count + "台作成済みです。");
}
ルール③:実行用クラスで、ルール②で設定したところを クラス名.変数名(メソッド名)に変更する。
例:
Car4 c1 = new Car4();
c1.display();
↓
Car4 c1 = new Car4();
**Car4**.display();