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
Post a Comment