LoginSignup
0
1

More than 5 years have passed since last update.

final の意味

Last updated at Posted at 2017-11-19

クラス

クラス宣言に final がついてる場合、そのクラスは拡張できない。(サブクラスが作れない)

メソッド

インスタンスメソッドの宣言に final がついてる場合、サブクラスでオーバーライドできない。

インスタンスフィールド

final なフィールドには一度しか代入できない。
final なインスタンスフィールドに代入するには下記2つの方法がある。

フィールドの宣言時に初期値を書く

class Qiita {
    final int value = 123;
}

コンストラクタで代入する

class Qiita {
    final int value;

    public Qiita() {
        this.value = 123;
    }
}

クラスフィールド

final なクラスフィールドに代入するには下記2つの方法がある。

フィールドの宣言時に初期値を書く

class Qiita {
    static final int value = 123;
}

static ブロックの中でフィールドに代入する

class Qiita {
    static final int value;

    static {
        value = 123;
    }
}

変数

一度しか代入できない。

引数

一度も代入できない。(メソッドが呼び出されたときに既に値が代入されているから)

0
1
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
1