When I study the SC of the algorithms, I firstly know static block.
What’s static block?
static{
}
why do we use it?
answer: to initialize the variables of static in the class.
when is it be execute?
answer: while the class that this static block belongs to is loaded to memory.
other
public class StaticBlock {
public static int a;
static {
System.out.println("static blocks");
a=3;
System.out.println("a "+a);
int a=1; *//homonym is allowed.*
System.out.println("a "+a);
}
public static void main(String[] args) {
StaticBlock sb=new StaticBlock();
System.out.println("a "+a);
}
}