static initialization block vs constructor java -


this question has answer here:

class prog {  static {     system.out.println("s1"); } prog() {     system.out.println("s2"); }  public static void main(string...args) {  prog p = new prog();  }  } 

output

s1 s2 

as per output, seems static initialization block gets executed before default constructor executed.

can tell rationale behind ?

strictly speaking, static initializers executed, when the class initialized.

class loading separate step, happens earlier. class loaded , initialized, timing doesn't matter of time. is possible load class without initializing (for example using the three-argument class.forname() variant).

no matter way approach it: class always initialized @ time create instance of it, static block have been run @ time.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -