• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is x being accessed from a static context when using anonymous inner classes?  RSS feed

 
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following example code



The compiler is complaining that x cannot be accessed from a non-static context



Given that AnonymousInnerClassesFlavour1.x is private, i am guessing that reference to x from the anonymous class is accessing AnonymousInnerClassesFlavour1Controller.x. But why is it saying that this is being access from a static context when i am creating a just in time object at the time i am access it?

Also, if i change the access modifier for AnonymousInnerClassesFlavour1.x and make it protected, the compiler does not complain. How does it decide which x it should use when there are two possibilities? In this situation, i thought it would have used the one local it i.e. AnonymousInnerClassesFlavour1Controller.x
 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that

in both codes are non static.

change to static and it will work.
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simple thing to do to investigate those questions would be to write that same code with the anonymous inner class declared inside an instance method instead of inside a static method.
 
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O. Ziggy wrote:Given that AnonymousInnerClassesFlavour1.x is private, i am guessing that reference to x from the anonymous class is accessing AnonymousInnerClassesFlavour1Controller.x.

Correct.
O. Ziggy wrote:But why is it saying that this is being access from a static context when i am creating a just in time object at the time i am access it?

But your "just in time object" is a different class. You need a AnonymousInnerClassesFlavour1Controller, and you have created a AnonymousInnerClassesFlavour1. And you can't access the x inside the AnonymousInnerClassesFlavour1, because it's private, and you're in a different class.
 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure how this question ended up here. I was intending to post it in the SCJP section.

I know what the error is saying. What i dont understand is why it is complaining that i am accessing it from a static context. Even though the inner class is being declared inside main, the actual reference to it is via an instance method. i.e the an1 instance.

Thanks in advance.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that's exactly the question I just answered, isn't it? It's an instance method of a totally unrelated class. You can't use an instance of AnonymousInnerClassesFlavour1 to access AnonymousInnerClassesFlavour1Controller.x.. Those are not the same class.
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just did the test that I suggested yesterday. And yes, you don't get a compile error if the nested class is instantiated inside an instance method.

So the "static context" which you didn't understand is the method "public static void main".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!