Originally posted by mark stone:
why does this give a compile error:
undefined variable r in System.out.println(r)
what is the rule for the static initializer block here ?
public class Test {
static {int r = 7;}
public static void main(String args[])
{
System.out.println(r);
}
}
In your static block, you create a local variable named r. When the static block completes, the variable r goes out of scope and no longer exists.
Try this instead:
public class Test {
static int r;
static { r = 7;}
public static void main(String args[])
{
System.out.println(r);
}
Rob