about static block
I write two class like this:
one is:
package test.prejspload;
public class StaticBlock
{
public StaticBlock(){
}
static{
System.out.println("this is static block");
}
public static void main(String[] args){
System.out.println("this is main");
}
}
another is :
package test;
import test.prejspload.StaticBlock;
public class StaticBlock2{
public static void main(String[] args){
StaticBlock b1 = new StaticBlock(); // mask or not
System.out.println("static block 2");
try {
Class staticClass = Class.forName("test.prejspload.StaticBlock");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
if mask the red line, StaticBlock2's result is :
static block 2
this is static block
if do not mask the red line, StaticBlock2's result is :
this is static block
static block 2
now, we can see that main block would be executed when initialize it's class.
at first, i think, maybe when 'import', static block has been exec,
actually, it's not.
just when initialize.