This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!