wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Static variables in inner class not allowed why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static variables in inner class not allowed why?" Watch "Static variables in inner class not allowed why?" New topic

Static variables in inner class not allowed why?

Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
In a non-static inner class, static final variables are allowed, but not static variables! why?

Joe Ess

Joined: Oct 29, 2001
Posts: 8836

Static fields are associated with a class rather than a particular instance of that class. A non-static inner class is associated with a particular instance of the class that contains it. It doesn't make sense to have the member variable longer-lived than the class that declares it.

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Steve Lovelace
Ranch Hand

Joined: Sep 03, 2003
Posts: 125
To be a bit more precise, static members are associated with a Class object. All classes loaded, even inner ones, will have exactly one Class object (ignoring multiple or badly-behaved classLoaders). So there is no logical reason for disallowing static members in inner classes. IMHO, the fact that they are not allowed leads to an unnecessarily complicated and foggy nested class specification.
And by the way, I hear the compiler is sneaking static members into inner classes as part of the assertion mechanism.
Chandra, you asked a good question.

The Inner that is named is not the true Inner.
I agree. Here's the link: http://aspose.com/file-tools
subject: Static variables in inner class not allowed why?
Similar Threads
Inner classes
Static instance variables and static methods
static members in inner classes
y static var r not allowed in INNER CLASSES ?
static modifier doubt