Static Block and Static variables initialized when the class loaded to JVM.
So, the order of execution is sequential.
public class StaticBlock {
static {
modifyIn = "static block 1";
System.out.println("static block 1");
}
static {
modifyIn = "static block 2";
System.out.println("static block 2");
}
static String modifyIn = "static variable";
public static void main(String args[]) {
// modifyIn = String value modified by :static variable
System.out.println("String value modified by :" + modifyIn);
}
}
Output:
static block 1
static block 2
String value modified by :static variable
public class StaticBlock {
static String modifyIn = "static variable";
static {
modifyIn = "static block 1";
System.out.println("static block 1");
}
static {
modifyIn = "static block 2";
System.out.println("static block 2");
}
public static void main(String args[]) {
// modifyIn = String value modified by :static block 2
System.out.println("String value modified by :" + modifyIn);
}
}
Output:
static block 1
static block 2
String value modified by :static block 2
No comments:
Post a Comment