• 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

Reg. Inner class & Nested class

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If i read statement like "A inner class is..."
then does it include all the below :
1. Top-level nested class
2. Non-static inner class
3. Local class
4. Anonmymous class
Then for a statement like " A nested class is .."
what all types does it include ?
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Angela, I read from Khalid's book that top-level nested class is not inner class. The reason is that they can be instantiated without the instance of its enclosing class.
Correct me if i am wrong.

------------------
Guoqiao Sun
Sun Certified Programmer for Java™ 2 Platform
try my mock exam¹² at my homepage.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Angela,
I, too, think the wording is ambiguous and have settled with the conclusion that "inner class" refers to a non-static inner class and nested class just refers to top-level-nested classes. This, however, is only a gut feeling!
kind regards,
james.
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Is the below statement correct :
"An inner class can be declared as static"
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes they can be static. Inner classes are known as nested classes.
If you have a copy of Vel's notes, Chapter 6 "Objects and Classes" is an excellent reference for breaking down the different types of inner classes. Here's the site http://www.geocities.com/velmurugan_p/
------------------
Percy Densmore
-SCJP2 Die Hard Student
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The tutorial for the Java Language at Sun uses the following terminology:
Nested class a class that is member of another.
Inner class a nested one that is not static.
I like to think that a top level class is one that is member of a package.
Expressions like top level nested class confused me, maybe is a nested class within a top level class?
Obviously the point is to use the terminology that is going to be used in the examination. Does anybody know what is like?
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then do i conclude something like this :


A nested class includes :
1. Top-level nested class( i.e declared static )
2. Non-static inner class
3. Local class
4. Anonymous class
An inner class includes :
1. Non-static inner class
2. Local class
3. Anonmymous class
But an inner class may be static as it one type of nested class.


Pls. correct if or append it anything if wrong.
Thanks
[This message has been edited by Angela Narain (edited September 15, 2001).]
 
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 Angela,
A top-level class is a class that is not a nested class (see JLS §8, 2nd paragraph
A nested class is any class declared within the body another class or interface (see previous reference)
An inner class is any nested class that is not declared static (see JLS §8.1.2
A static nested class is sometimes referred to as a top-level nested class which is a bit of an oxy-moron. Think the important point to remember is that a static nested class is not an "inner class".
Hope that helps.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Referring to the below
An inner class is any nested class that is not declared static
(see JLS �8.1.2 )
How does the above line justify .
" an inner class may be declared static"
 
james hoskins
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Angela,
Where did you get the quote " a nested class may be declared static."?
regards,
james.
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is one of the option answers in Marcus Green's Exam#2
Which of the following statements are true
1) An inner class may be defined as static
2) There are NO circumstances where an inner class may be defined as private
3) An anonymous class may have only one constructor
4) An inner class may extend another class

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
To the best of my recollection all nested classes are inner classes but not all inner classes are nested classes. Reson being the class defined with in a method is inner class but it is not a nested class.
Correct me if I'm wrong.
Thanx.

Jennifer.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jennifer
Reread what Jane said before because you are no right. ;-)
If a class is whithin a method it is clearly within the class that declared the method, so it is a nested class. If the method is static the nested class will be static, and thefore not inner.
 
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
Hi Angela,
Unfortunately there's been alot of confusion over the terms ever since Sun first introduced inner classes. The documentation wasn't very clear.
For a good discussion on the topic see Joshua Bloch's response in this Java Intermediate post
Hope it helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jane...
That thread happened to be an eye-opener indeed.
Shyam
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic