I am preparing for OCJA exam where I came across the concept of static blocks. I was trying the different combination where I have noted that nesting of instance blocks are allowed but nesting of static blocks are not allowed. Why nesting of static blocks are not allowed? Unfortunately I am not able to find the online resource where this reason can be explained.
Below is nesting of instance block which compiles fine.
class Test {
{
{
System.out.print("Instance Block One");
{
System.out.print("Nested Instance Block");
}
}
}
public static void main(String[] args){
System.out.print("Main Method");
}
}
Below is nesting of static block which fails at compile time.
class Test {
static{
static{
System.out.print("Static Block One");
static{
System.out.print("Nested static Block");
}
}
}
public static void main(String[] args){
System.out.print("Main Method");
}
}