• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static members and local classes

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This raised another question to me. Why can't we declare static members for local class ?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The issue with "why" questions, is that, generally, there is a really easy answer, and a maybe impossible answer. The easy answer with most of these questions is "because that is how it is defined in the JLS". The "why" its that way, ie. the hard answer, would require that it be answered by one of the Java designers. All that us mortals can do is speculate.

So... Why? Because that is how it is defined in the JLS. Anymore than that would be speculation.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here is my speculation... I think the spec writers got lazy.

I think that the designers knew that they would have to specify how static inner variables (and methods) are related to the static outer variables (and methods) -- and it was a can of worms that they didn't want to open. So, they made it easy. The static modifier are for top level classes and nested classes. With one sentence, they saved from having to write a few chapters defining the behaviors of static inner class fields and methods.

Of course, this is my speculation. I would love to hear everyone else's opinion. Or better yet, if someone know the reason from the source.

Henry
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From Programmer's Guide to Java SCJP Certification:
The local classes cannot have static members, as they do not provide class-specific services.
However, final static fields are allowed, as these are constants.
 
reply
    Bookmark Topic Watch Topic
  • New Topic