In depth understanding of initialization of final variables

  • 2020-04-01 02:00:09
  • OfStack

The initialization position of the final variable
One is where it's defined, which means that when a final variable is defined,
The second is in the constructor. And before Java1.1, you could only give values at definition time.
The third one is {} or static{} in the initializing code block.

public class InitOrder {
    {
        System.out.println("before---field");
        //System.out.println("d1="+d1);
        d1 = 3;
        //System.out.println("d1="+d1);
    }
    static {
        System.out.println("before---static field");
        //System.out.println("d2="+d2);
        d2 = 3;
        //System.out.println("d2="+d2);
    }
    final int a1 = 1;
    final int b1;
    final int c1;
    final int d1;
    //final int e1;
    static final int a2 = 1;
    //static final int b2;
    static final int c2;
    static final int d2;
    //static final int e2;

    {
        System.out.println("after---field");
        //System.out.println("c1="+c1);
        c1 = 4;
        System.out.println("c1="+c1);
        //e2 =3;
    }
    static {
        System.out.println("after---static field");
        //System.out.println("c2="+c2);
        c2 = 4;
        System.out.println("c2="+c2);
        //e1 = 3;
    }
    public InitOrder() {
        b1 = 2;
        //b2 = 2;
    }
    public static void main(String[] args) {
        InitOrder order = new InitOrder();
        System.out.println("c1="+order.c1);
        System.out.println("c2="+order.c2);
        System.out.println("d1="+order.d1);
        System.out.println("d2="+order.d2);
    }
}

Note: all comments above are syntax errors
Output results:
Before -- - the static field
After - static field
C2 = 4
Before - field
After - field
C1 = 4
C1 = 4
C2 = 4
D1 = 3
D2 = 3
Result analysis:
1. Compared with a1, there is basically no problem with a2, which is initialized when defined
2. Compared with b1,b2 is initialized in the constructor. B1 has no problem and b2 has problems
3. Comparing with c1,d1 finds that there is no problem in initialization, but the problem is to use output statement. For d1 no matter where in the initialization code block and output statements will be an error, this is because the d1 in the initialization code block position in front of the definition of the variable d1, and Java variable initialization sequence is seen in Java variable initialization sequence, common variables and initialization code block initialization sequence is according to the position of successively so output with the d1 variables, so wrong, but one thing I don't understand, why here is initialized d1 is not an error, but also can call it in the main function, isn't this also want to have a look at the Java virtual machine? There is no problem with adding an output statement to c1. The problem with the previous addition is easy to understand, which is that it was not initialized before the big one.
4. For c2,d2 is the same thing as 3
5. For e1, it is obviously not good to put it in a static code block, because the static code block is loaded first, and e1 has not been added yet
6. For e2, too, normal code blocks load later than static variables, so they don't work.

Related articles: