This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Why inner class can't have static variable 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 » Java in General
Bookmark "Why inner class can Watch "Why inner class can New topic
Author

Why inner class can't have static variable

bagaria kushal
Greenhorn

Joined: Mar 29, 2011
Posts: 19
Please explain why inner class can't have static variables???
and if the variable is static final then it compiles fine. why?
Andrey Kozhanov
Ranch Hand

Joined: Mar 12, 2010
Posts: 79
Just think about it. Static block is executed during class initialization, and you cannot initialize a non-static inner class without having an instance of the enclosing class, that's the reason.

if the variable is static final then it compiles fine

That's not exactly true. According to specification inner classes may not declare static members, unless they are compile-time constant field. Consider the following code:
bagaria kushal
Greenhorn

Joined: Mar 29, 2011
Posts: 19
Can you please explain why compile time constants can be marked as static inside inner class
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why inner class can't have static variable
 
Similar Threads
Why should the parameter be final?
Double-standard for inner class if it is non-static vs if it is static
inner class and static variables
static declaration in inner class
static modifier doubt