Why isn't a qualified static final variable allowed in a static initialization block?

佐手、 提交于 2019-11-26 04:45:47

问题


Case 1

class Program {
    static final int var;

    static {
        Program.var = 8;  // Compilation error
    }

    public static void main(String[] args) {
        int i;
        i = Program.var;
        System.out.println(Program.var);
    }
}

Case 2

class Program {
    static final int var;

    static {
        var = 8;  //OK
    }

    public static void main(String[] args) {
        System.out.println(Program.var);
    }
}

Why does Case 1 cause a compilation error?


回答1:


The JLS holds the answer (note the bold statement):

Similarly, every blank final variable must be assigned at most once; it must be definitely unassigned when an assignment to it occurs. Such an assignment is defined to occur if and only if either the simple name of the variable (or, for a field, its simple name qualified by this) occurs on the left hand side of an assignment operator. [§16]

This means that the 'simple name' must be used when assigning static final variables - i.e. the var name without any qualifiers.




回答2:


Apparently this is a cheap syntactic trick to limit definite (un)assignment analysis within the class itself.

If the field is syntactically qualified with a class name, the code is usually in another class, where the analysis cannot reach.

This trick fails in your example. Other examples of oddity:

static class A
{
    static final int a;
    static
    {
        // System.out.println(a); // illegal
        System.out.println(A.a);  // compiles!
        a = 1;
    }
}

If they had more resources, they probably would've made a finer rule. But we can't change spec now.



来源:https://stackoverflow.com/questions/13778650/why-isnt-a-qualified-static-final-variable-allowed-in-a-static-initialization-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!