This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why inner class can't have static variable

 
bagaria kushal
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 79
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please explain why compile time constants can be marked as static inside inner class
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic