java - Why static block are executed later? -
p.s :
this question has been edited few times previous code doesn't demonstrate problem. there answers may not make perfect sense against edited question
i have public class named son.java
package com.t; public class son extends father { static int i; static { system.out.println("son - static"); = 19; } { system.out.println("son - init-block"); } public static void main(string[] args) { //son s = new son(); int a[] = new int[2]; system.out.println(a[5]); } } class father { static { system.out.println("f - static"); } { system.out.println("f - init-block"); } }
when run program 1st time:
output is:
exception in thread "main" java.lang.arrayindexoutofboundsexception: 5 @ com.t.son.main(son.java:19) f - static son - static
and later when run program (order of output random
)
output is:
f - static son - static exception in thread "main" java.lang.arrayindexoutofboundsexception: 5 @ com.t.son.main(son.java:19)
i have read static
blocks executed classes initalised.
but why exception has come first here , static block executed?
i using eclipse
run program. can explain?
the exception doesn't happen first, seeing printout of exception first.
had exception happened first, never have seen rest of output.
the reason have output both system.err
(from exception) , system.out
in program. order in these printed screen not defined, therefore can them in different order.
Comments
Post a Comment