• 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

Local inner class declaration within static method

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I just need a better explanation of why its ok to have a "local inner class declaration" within a static method vs its not legal to invoke an inner class within the same static method ie:
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can reference class Second from a static method if class Second is also static.

"a non-static variable this cannot be referenced from a static context" even if the non-static variable is another (inner) class.
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You can reference class Second from a static method if class Second is also static.


I assume this is the case since class Second is defined within a static method? If it were defined outside the static method, then the inner class could be non-static, yet still invoked by a static method:

Not sure if that theory is absolutely correct, but I have witnessed code that does illustrate the definition of a non static local inner class within a static method:

Invoked from another method like this correct?
outer.LocalinStatic myInner = new Outer(). new LocalinStatic()
This is the last issue I have with local inner classes, I appreciate you all helping me out.
 
reply
    Bookmark Topic Watch Topic
  • New Topic