java - Static initializers and Constant Wrapper Variables -


here quick snippet of code. can relate jls section 12.4 , 12.5 class loading , initialization process. class loaded if accessing static variable of class not constant or may access static method. in case, declaring variable final, remove final attribute, , check class loaded , static initializer run. below modified code

class staticfinaldemo1  {         //static final int var= 100;         static int var= 100;              static final void test()  {                system.out.println("static final method test");         }         static  {                    system.out.println("static initializer");         }     }       class staticfinaldemo2 {              public static void main(string[] args)   {                  system.out.println(staticfinaldemo1.var);                 //staticfinaldemo1.test();             }         }     

now point if modify final statement , replace following statement.

 static final integer var= 100;  

the static initializer gets loaded. variable constant. why loading static initializer in case? because using wrapper object , instance initialized when refer in class usage? please clarify concept.

ben

no, variable not compile-time constant. constant can of primitive type or of type string. integer not qualify.

see jls §15.28:

a compile-time constant expression expression denoting value of primitive type or string not complete abruptly , composed using following: [...]


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -