• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that

in both codes are non static.

change to static and it will work.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
reply
    Bookmark Topic Watch Topic
  • New Topic