To distinguish between an instance field and a local variable with the same name, we can access the field with the this. prefix this. :
class Test { public final Foo x; public Test(Foo x) { this.x = x; } }
I am trying to do the same in a static context using qualification access with a class name:
import java.util.*; class Test { public static final Map<String,Object> map; static { Map<String,Object> map = new HashMap<>();
The compiler does not want to have anything to do with this code. I have several variables like this, and for all of them, he continues to shout: "it is impossible to assign a value to a final variable." If I do not assign it, he will instead complain about a "variable not initialized." If I assign a static field at the beginning and try to make the map immutable afterwards, she complains that "the variable may have already been assigned." It does not suit anything.
Is this a flaw in the language or a bug in the compiler? What is the best way to crush the compiler at runtime?
source share