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
a static initializer declared in class executed when class initialized
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
Post a Comment