This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Is x being accessed from a static context when using anonymous inner classes? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Is x being accessed from a static context when using anonymous inner classes?" Watch "Is x being accessed from a static context when using anonymous inner classes?" New topic
Author

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

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

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
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

The problem is that

in both codes are non static.

change to static and it will work.


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

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.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2778
    
    2
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

Joined: Oct 02, 2005
Posts: 430

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

Joined: Mar 05, 2008
Posts: 2778
    
    2
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
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

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".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is x being accessed from a static context when using anonymous inner classes?
 
Similar Threads
Possible error in CJ2CSG?
Accessing primitives in anonymous inner class
lil explanation requied ..
static nested class
Why a Static method cannot refer to an instance variable?