This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

How do you classify a method-local inner class defined inside a static method?  RSS feed

 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Method-local inner classes defined inside instance methods are inner classes (their instances are tied to instances of the outer class.)
However, when you define a method-local inner class inside a static method, there is no instance of the outer class involved. I thought this would mean that in that case the method-local inner class would become a static inner class, but that's not the case (I tried to put a static variable declaration inside such a class and I got a compiler error.) How are these classes classified then? Or is it worthless to seek a classification, as they are what they are?

Thanks!
 
author
Sheriff
Posts: 23568
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It is a bit weird isn't it? I always think of it as -- a method local inner class defined in a static method can only access the static members of the outer class. I don't think that this makes the inner class a static inner (nested) class though.

Henry
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your answer, Henry. I'm glad I'm not the only one that thinks this is a little weird (or irregular, if you will.) And yes, you are right that a method-local inner class defined inside a static method doesn't become a static class, because it is not possible to declare static variables in it which are not compile-time constants (which would be allowed if it were actually a static class.)

Thanks again!
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can make the variable static final and declare it inside of the block
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!