0
0

java static イニシャライザとstaticフィールド

Last updated at Posted at 2024-02-02

java static イニシャライザ

静的初期化ブロックは、括弧{}で囲まれ、前にキーワードstaticが付いているコードブロックです。
static イニシャライザは、クラスロードされる際に、実行されるブロックです。

staticフィールド

staticフィールドはプログラムが実行される時に生成され、すべてのインスタンスが共有します。

実装

public class StaticTest {
  public static void main(String[] args) {
    System.out.println(AAA.m);
  }
}

class AAA {
  static {
    m = 300;
  }
  static int m = 100;
}

実装結果

100

Process finished with exit code 0
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