In JSL 3.0, chapter 12.4.1 "When Initialization Occurs" it says:
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, and an assert statement (§14.10) lexically nested within T is executed.
But what happens if the field is a constant variable?
A test* in the debugger shows, that it is initialized the same, when the field is used.
Can someone clear this up?
* for test I used this code with Sun JRE 1.6.0u17, in Eclipse v3.5.1:
Are you confusing "class initialization" with "object initialization"?
I changed line 3 of the code to :
Again, the class is initialized when line 11 in the first code snippet is executed. No matter if the field is final or not.
I understand that it is initialized in the case when the field is not a constant, because that is what the JLS says.
My question is: in the case when the field is a constant, where is it defined when the initialization happens?
I am writing a program and can not rely on accidental implementation behavior, but need confirmation in the specs.