• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Inner classes cannot have static methods and non final static variables  RSS feed

 
kavitha Jayachandran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why is it that inner classes can have static final variables but cannot have non final static variables as well as static methods?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know. You may never find out unless one of the original language designers turns up here. I found a couple of posts about it: 1 2 (Both on SO). No 2 suggests that the inner class being not static is implicitly part of the surrounding object, so should only have instance members. I don't remember seeing an explanation in the Java™ Tutorials.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This, I think, is as near as the Java™ Tutorials get:-
the Java™ Tutorials wrote:because an inner class is associated with an instance, it cannot define any static members itself.
 
kavitha Jayachandran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of a static member as a member that is associated with a top-level class. An inner class is not a top-level class; it is a "special" class that is associated with an instance.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!