Meaning of final

class

If the class declaration has final, the class cannot be extended. (Cannot create subclass)

Method

If the instance method declaration has final, it cannot be overridden in the subclass.

Instance field

You can only assign to a final field once. There are two ways to assign to a final instance field.

Write the initial value when declaring a field

class Qiita {
    final int value = 123;
}

Assign in constructor

class Qiita {
    final int value;

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

Class field

There are two ways to assign to a final class field:

Write the initial value when declaring a field

class Qiita {
    static final int value = 123;
}

Assign to a field in a static block

class Qiita {
    static final int value;

    static {
        value = 123;
    }
}

variable

Can only be assigned once.

argument

Cannot be assigned even once. (Because the value was already assigned when the method was called)

Recommended Posts

Meaning of final
[Meaning of model: of form_with]
Meaning of writing an import statement
12 of Array
final modifier
[Java] Use of final in local variable declaration
Initialization timing of member variable with final specified