About Question com.enthuware.ets.scjp.v6.2.359 :
Posted: Wed Feb 22, 2012 1:04 pm
Hello,
the Explanation box for this question says : "Unlike popular belief and unlike mentioned in various books, anonymous class can never be static. Even if created in a static method."
If that were true, then it would be possible for the anonymous class to access a non-static member (such as an instance variable) declared inside the outer class, as in this code:
class A
{
}
public class TestClass
{
int outerInt = 7;
public class A
{
public void m() { }
}
class B extends A { }
public static void main(String args[]) {
new TestClass().new A() {
public void m() {
System.out.println("OuterInt is "+outerInt);
}
};
}
}
But this code won't compile because the compiler will detect an access of a non-static resource from a static context.
Perhaps didn't get the meaning of that explanation correctly; please shed some light on this.
the Explanation box for this question says : "Unlike popular belief and unlike mentioned in various books, anonymous class can never be static. Even if created in a static method."
If that were true, then it would be possible for the anonymous class to access a non-static member (such as an instance variable) declared inside the outer class, as in this code:
class A
{
}
public class TestClass
{
int outerInt = 7;
public class A
{
public void m() { }
}
class B extends A { }
public static void main(String args[]) {
new TestClass().new A() {
public void m() {
System.out.println("OuterInt is "+outerInt);
}
};
}
}
But this code won't compile because the compiler will detect an access of a non-static resource from a static context.
Perhaps didn't get the meaning of that explanation correctly; please shed some light on this.