Originally posted by vinayak manda:
public class Test {
static {int a=5;}
public static void main(String args[]{
System.out.println(a);
}
}
Is this legal,is it true that a variable declared in a static initializer is not accessible outside the enclosing block.
Extend this concept beyond a static variable to any variable that is declared within the curly braces. The curly braces provide the scoping context for the variable lifetime. It really doesn't matter if it is a static variable or not. For example, if I add a bit more code to your example:
I will get the same error as I did for a.