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.
About Question com.enthuware.ets.scjp.v6.2.359 :
Moderator: admin
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.359 :
In this case, the inner class is unable to access outerInt, not because the inner class is static but because the inner class created in a static context (in this case a static method.)
There is not just one rule that applies while determining whether you can access a field or not. There are multiple rules that determine valid access.
An inner class can only access those fields that are available in its context. In this case, the main method is static and it doesn't have access to outerInt, so the inner class created in the main method also doesn't have access to outerInt.
This doesn't mean that the inner class itself is static.
HTH,
Paul.
There is not just one rule that applies while determining whether you can access a field or not. There are multiple rules that determine valid access.
An inner class can only access those fields that are available in its context. In this case, the main method is static and it doesn't have access to outerInt, so the inner class created in the main method also doesn't have access to outerInt.
This doesn't mean that the inner class itself is static.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 57
- Joined: Sat Mar 01, 2014 1:48 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.359 :
According to the commentary for option 3:
although I don't think where would in practice be any way to extend the anonymous class.
Using reflection to test that, it doesn't seem to be the case, (assuming I'm testing what I think I am!)Anonymous classes are implicitly final.
Code: Select all
import java.lang.reflect.*;
class AnonymousTest{
class A{}
static void report(Object o){
Class myClass = o.getClass();
System.out.printf("%s\n", Modifier.toString(myClass.getModifiers()));
}
public static void main(String[] args){
report((new AnonymousTest()).new A(){}); //static but not final
}
}
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.359 :
But it is final as per the specification: https://docs.oracle.com/javase/specs/jl ... tml#15.9.5
If you like our products and services, please help us by posting your review here.
-
- Posts: 57
- Joined: Sat Mar 01, 2014 1:48 pm
- Contact:
Re: About Question com.enthuware.ets.scjp.v6.2.359 :
ok. Thank you.admin wrote:But it is final as per the specification: https://docs.oracle.com/javase/specs/jl ... tml#15.9.5
Who is online
Users browsing this forum: Bing [Bot] and 99 guests