• 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

Method-Local Static Inner Class

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to define as static an inner class that is declared within a method?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, a local class of a non-static method always is non-static, as far as I know.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just as Ilja has said, it's not possible to delcare a local class with a static modifer. See Jave Language Specification 14.3 Local Class Declarations for explanation.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joyce's short answer is all you really need here. But Ilja's post would seem to raise the question: what about a local class declared in a static method? Answer: this is also non-static. You can't declare it as static, and it's not implicitly static, therefore it's still an inner class (just like a local class in a nonstatic method). However a local class declared in a static method is considered to be in a static context. Such a class is like a static nested class in that it has no access to nonstatic members of the enclosing class, and no access to a "this" reference for the enclosing class. (I.e. no OuterClass.this may be used.) However a local class in a static context is still considered an inner class, and shares other characteristics and limitations of inner classes - such as not being allowed to declare static members.

So, a local class in a static method is sort of a cross between a static nested class and a "traditional" (completely nonstatic) inner class. The JLS chose to describe it as an inner class with special rules. Which means, among other things, you can never declare a local class as static, even if it's declared inside a static method.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim, thanks for the clarification!
 
Philip Pomario
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the great explanations, guys!
 
Philip Pomario
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just passed the SCJP1.4 exam and wanted to post this appreciation note to thank everyone who helped me understand Java a little better. Without your help this personal achievement wouldn't be possible.
reply
    Bookmark Topic Watch Topic
  • New Topic