Initializing static final variables in java

Armen Arakelyan :
public class Test {

    private static final int A;

    static {
        A = 5;
    }
}

This way of initializing static final variable A works okay .

public class Test {
    private static final int A;

    static {
        Test.A = 5;
    }   
}

This way gives compile error "Cannot assign a value to final variable 'A'.

Why?

Makoto :

Specified by the rules for Definite Assignment:

Let C be a class, and let V be a blank static final member field of C, declared in C. Then:

  • V is definitely unassigned (and moreover is not definitely assigned) before the leftmost enum constant, static initializer (§8.7), or static variable initializer of C.

  • V is [un]assigned before an enum constant, static initializer, or static variable initializer of C other than the leftmost iff V is [un]assigned after the preceding enum constant, static initializer, or static variable initializer of C.

In layman's terms:

  • Using a static initializer to initialize a static final field by referencing its simple name is OK since that field is definitely assigned after the initializer. Effectively, there's context into the class in which the static initializer is declared in, and you are not performing any illegal assignment by referring to the field by its simple name; instead, you're fulfilling the requirement that the field must be definitely assigned.

  • Using a static initializer to initialize a static final field by referencing its qualified name is illegal, since the class must be initialized when referring to a static property of it (in your case, Test.A must be initialized prior, and A is assigned the default value of null upon completion of initialization).

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=461588&siteId=1
Recommended