0
0

More than 1 year has passed since last update.

staticメソッド

Posted at

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();

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0