• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

static keyword

 
bindu ramesh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
" a nested top level class cannot declare any static variables, static methods or static initializers " why is this statement true? Especially in case of static initializers
[This message has been edited by bindu ramesh (edited March 21, 2001).]
 
Slightly Uber
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmmm, not sure on this one. I'm studying from the "A Programmer's Guide to Java Cert" by Khalid Mughal and it states:
"...a top level static nested class can define both static and instance members like any other package level class. However, its code can only directly access static members in its enclosing context."
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure where you are getting your quote from but this is from the JLS:


8.1.2 Inner Classes and Enclosing Instances
An inner class is a nested class that is not explicitly or implicitly declared static. Inner classes may not declare static initializers (�8.7) or member interfaces. Inner
classes may not declare static members, unless they are compile-time constant fields (�15.28).


and


Inner classes may inherit static members that are not compile-time constants even though they may not declare them. Nested classes that are not inner classes may
declare static members freely, in accordance with the usual rules of the Java programming language.
Member interfaces (�8.5) are always implicitly static so they are
never considered to be inner classes.


Inner classes are related to instances of the outer class, and therefore can't declare static things because static code must relate to the whole class.
Static inner classes (aka nested top level classes) CAN declare static stuff.
bindu, I think you must have mis-quoted something
[This message has been edited by Cindy Glass (edited March 21, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic