• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method-Local Inner Classes & the static modifier  RSS feed

 
Ranch Hand
Posts: 693
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Top-level classes cannot be declared static, inner classes are recommended to be declared static, but method-local classes cannot be declared static.

Can somebody explain why things are this way?

Kaydell
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Kaydell Leavitt:
... inner classes are recommended to be declared static...


According to JLS 8.1.3, "An inner class is a nested class that is not explicitly or implicitly declared static."
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

1) Declaring a top level class static dont mean anything because you can declare member variable as static and call that on class name with dot operator.

2) Now inner class is member variable so you can declare it static so that you dont have to create the instance of outer class to call it. And because it is member variable of the class all the rules for member variable apply's to it also.

3) local classes is local to a method so the rules for local variable will be applicable to it.

i think i have answer it all.

please let me know if i have missed something or you not getting what i am trying to convey.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!