Textual order of static variables and initialization order in Java -
i spend 5 minutes find duplicate in so.
my question simple. following code work?
public class lexicalorderstatic { private static integer a1 = inita1(); private static integer a2 = inita2(); private static integer inita2(){ return new integer(5) / a1; } private static integer inita1(){ return new integer(5); } public integer geta1(){ return new integer(a2); } public static void main(string[] args) { lexicalorderstatic lexluthor = new lexicalorderstatic(); system.out.println(lexluthor.geta1()); } }
in java can sure a1 always initialized before a2 ?
thanks. dw ok if asked or if simple.
in java can sure a1 initialized before a2 ?
yes, because specification (section 12.4.2) guarantees (emphasis mine):
next, execute either class variable initializers , static initializers of class, or field initializers of interface, in textual order, though single block.
note constants initialized earlier non-constants (step 6 compared step 9 quoted above).
Comments
Post a Comment