Java why static prints out text first than method -


just wondering why static 1 print out first rather method.

code:

public class testme {     static {         system.out.println("d");     }      {         system.out.println("b");     }       public void printme() {         system.out.println("z");     }      public static void main(string []args) {         new testme().printme();     }  } 

output:

d b z 

you have 2 types of initializer block in class

one static initializer executed time class initialized

8.7. static initializers

a static initializer declared in class executed when class initialized 

8.6. instance initializers

two instance initializers executed when instance of class create

an instance initializer declared in class executed when instance of class created  

those jls documentation

so static initializer called directly when class initialized vs instance initializers called when instance of class created static initializer executed first.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -