• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Inner Class definition per JLS

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a direct quote from JLS"

What's right/wrong?!!
Is my Inner class NOT REALLY AN INNER CLASS??
Thanks
[This message has been edited by Madan, Gopal (edited November 05, 2001).]
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code, it is a Top level Nested class.
One kind of nested inner class.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Madan,
JLS � 8.1.2 states An inner class is a nested class that is not explicitly or implicitly declared static.
So technicaly a nested class that is declared static is not not an inner class. Exactly what they are isn't really clear from the documentation.
'Top-level nested' seems to be the clearest way to describe them. Although you'll also see 'top-level static' being used.
Should have said "static member class" is the best way to describe them. See Joshua Bloch's comments in this thread


Important thing is to understand the difference in scope between a 'static' class and an 'inner class'.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform

[This message has been edited by Jane Griscti (edited November 06, 2001).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding to what Jane has said....
The following classes are considered to be nested classes:

  • Top-Level Nested classes and Interfaces.
  • Non-Static inner classes
  • Local classes
  • Anonymous classes

  • The first item is considered to be at the top level. The remaining three are considered to be inner classes. Effectively, all inner classes are nested classes but not vice versa.
    Hope this helps
    Shyam
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I posted this on the Marcus Green Discussion Forum a few days ago:-


Testing objectives for the Programmer for Java 2 Platform state:
(Overloading, Overriding, Runtime Type, and Object Orientation)
"Write code to construct instances of any concrete class including normal top level classes, inner classes, static inner classes, and anonymous inner classes".
Now according to 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."
Also, it states under (8.5.2 Static Member Type Declarations)
"The static keyword may modify the declaration of a member type C within the body of a non-inner class T. Its effect is to declare that C is not an inner class."
There appears to be a stark contradiction between the JLS and the testing objectives for the Programmer for Java 2 Platform, which states the existence of Static Inner Classes.
According to my interpretation of the JLS, this entity that is referred to as a "Static Inner Class" should actually be called a Static Member Class.
Now my problem is this. If you get a question in the exam that poses the following: -"Can an Inner Class be static?" According to JLS (8.1.2 Inner Classes and Enclosing Instances) the answer is no. (I have seen many sources that state the opposite!! What answer do I need to give to collect the marks?) Nested Member Classes however can be static, coderanch, protected, private, abstract and final.
Furthermore, many sources state that nested top-level classes are static inner classes, but again the JLS, contradicts this.
"A nested class is any class whose declaration occurs within the body of another class or interface. A top level class is a class that is not a nested class."
Also could somebody confirm or correct the following hierarchy:-
* Top Level Classes
* Nested Classes
********* Member Classes
**************** Static Member Classes
**************** Inner Classes
************************ Non-static member classes
************************ Local Classes
************************ Anonymous Classes

Rosie


 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The best discussion I've seen on this topic is in this thread
In particular, read the comments by Joshua Bloch (author of Effective Java).
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited November 06, 2001).]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a thought:
I think that with respect to nested and inner classes we should always utilize the same terminology that JLS. Otherwise it is as we were talking in diferent languages.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rosie Nelson:
Now my problem is this. If you get a question in the exam that poses the following: -"Can an Inner Class be static?"

I guarantee that you will not see that question on the SCJP2 exam.

Also could somebody confirm or correct the following hierarchy:-
* Top Level Classes
* Nested Classes
********* Member Classes
**************** Static Member Classes
**************** Inner Classes
************************ Non-static member classes
************************ Local Classes
************************ Anonymous Classes


More like this:
* Top Level Classes

* Nested Classes
****** Member Classes
*********** Static Member Classes (scope similarities to static member fields and static member methods)
*********** Instance (Non-static) Member Classes (scope similarities to instance (member) fields and instance methods)
****** Local Classes - classes inside a method - (scope similarities to local variables)
*********** Anonymous Classes (a specific type of local class)

 
Rosie Nelson
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for helping me to clear that up!!
Having said that, I think it is very important that we all stick to the terminology used in the JLS. Any erroneous language should be corrected because of its potential for confusing examinees (myself included).
The java certification, being a key area of credibility for "Java Expertise" should update its "Sun Certified Programmer for Java 2 Platform Success Guide" where they make reference to "Static Inner Classes".
I have also seen a question here, at the Java Ranch Roundup that should be reworded.
"(#235) TRUE OR FALSE: a static inner class (considered a top level nested class) can not access non-static variables of the outer class."
Remember, according to the JLS:
"A nested class is any class whose declaration occurs within the body of another class or interface. A top level class is a class that is NOT a nested class."
And
(8.1.2 Inner Classes and Enclosing Instances)
"An inner class is a nested class that is not explicitly or implicitly declared static."
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic