This week's book giveaway is in the iOS forum.
We're giving away four copies of Classic Computer Science Problems in Swift and have David Kopec on-line!
See this thread for details.
Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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: 23484
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!