LoginSignup
0
0

More than 3 years have passed since last update.

Javaのクラスメソッド

Last updated at Posted at 2020-06-01

クラスメソッド

クラスに属するメソッドのことです。
クラスメソッドの定義は、「public static 戻り値の型 メソッド名()」です。
クラスメソッドはインスタンスを生成しない状態でも呼び出せます。
クラス名の頭文字は大文字です。
【例】Personクラスとします

Person.java
class Person {
  public static 戻り値の型 メソッド名() {
    // メソッドの処理
  }
}

【例】

Main.java
class Main {
  public static void main(String[] args) {

    省略

    Person.printCount();
  }
}
Person.java
class Person {
  public static int count = 0;

   省略

  public static void printCount() {
    System.out.println("合計は" + count + "です");
  }
}
0
0
0

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