Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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
Posts: 23810
140
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!