简体   繁体   中英

Static final variable along with static initializer block

I made a class that contains:

  1. static final variable
  2. static initializer block with a System.out.println() statement

If I call the static final variable from another class, the static block does not execute.

As far as I know, the static initializer block executes when the class is loaded in memory.

In this case, what is happening at the memory level?

Is the class not loading in memory? If not, where do other classes get the address of the final static variable?


Case 1: static block does NOT execute

class Test2 {
    static final int a = 20;

    static {
        System.out.println("one");
    }
}

Case 2: static block does execute

class Test2 {
    static final int a;

    static {
        a = 20;
        System.out.println("one");
    }
}

Output

class Test {
    public static void main(String[] args) {
        System.out.println(Test2.a);
    }
}
  • Case 1:

     20 
  • Case 2:

     one 20 

So what is happening at both levels?

  1. A static final field is a compile-time constant and its value is hardcoded into the destination class without a reference to its origin;

  2. therefore your main class does not trigger the loading of the class containing the field;

  3. therefore the static initializer in that class is not executed.

See the magic removing final from definition. You will see static initialiser executing

My guess is that your field is of either a primitive type or String , and is initialized with a compile-time constant expression.

For static final fields initialized with a constant expression (and only such fields) - any code which refers to the field will have the constant value baked into it, rather than going via the static field which would cause class initialization. The "constant expression" part is important though. We can see this with a small test app:

class Fields {

    public static final String CONSTANT = "Constant";
    public static final String NON_CONSTANT = new String("Non-constant");

    static {
        System.out.println("Initializing");
    }
}

public class Test {
    public static void main(String arg[]) {
        System.out.println(Fields.CONSTANT);
        System.out.println(Fields.NON_CONSTANT);
    }
}

The output is:

Constant
Initializing
Non-constant

Accessing the constant field does not require initialization, but accessing the non-constant one does. Using a non-final field would have the same effect: it would no longer count as a constant, basically.

The information about "this is a constant" gets baked into the class declaring a field. For example, using javap -c Fields we see the two fields:

public static final java.lang.String CONSTANT;
  flags: ACC_PUBLIC, ACC_STATIC, ACC_FINAL
  ConstantValue: String Constant

public static final java.lang.String NON_CONSTANT;
  flags: ACC_PUBLIC, ACC_STATIC, ACC_FINAL

Note the ConstantValue part of the CONSTANT field metadata, which is missing from the NON_CONSTANT field metadata.

See section 15.28 of the JLS for more on what constitutes a constant expression.

Section 12.4.1 of the JLS specifies when a class is initialized:

A class or interface type T will be initialized immediately before the first occurrence of any one of the following:

  • T is a class and an instance of T is created.

  • T is a class and a static method declared by T is invoked.

  • A static field declared by T is assigned.

  • A static field declared by T is used and the field is not a constant variable (§4.12.4) .

  • T is a top level class (§7.6), and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed.

(Emphasis mine.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM