aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Nested & Inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Nested & Inner class" Watch "Nested & Inner class" New topic
Author

Nested & Inner class

Shah Chunky
Ranch Hand

Joined: Dec 27, 2000
Posts: 116
Hello Friends...
Can someone tell me what is the difference between Inner Class, Nested Class & Top level nested Class.
Thanks in advance.

Shah Chunky - Sun Certified Java2 Programmer.
Joseph Russell
Ranch Hand

Joined: Jan 08, 2001
Posts: 290
An Inner class is a class declared inside a class:


Joe

[This message has been edited by Joseph Russell (edited February 16, 2001).]
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
Joseph,
In your code example, would it compile and run if you removed the line "Outerclass 0 = new Outerclass();"?
Or must you create a separate instance of the outer class before creating a "new Outerclass().innerclass();"?


<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
The phrase nested class usually refers to a static inner class.
Shrini Kulkarni
Ranch Hand

Joined: Jan 12, 2001
Posts: 63
Hi,
Top Level Nested inner class : static inner class - defined just like any other member variable. Declared between open and close braces of the class definition.
Inner class : Refers to non-static inner class - defined just like any other member variable. Declared between open and close braces of the class definition.
Local class (inner): a class defined within a code block like in a method.
Anonymous inner class : a class defined without a class name and used at a place where an object reference is needed.
Regards
Shrinivas

Sun Certified programmer for Java2 platform.<BR>Shri_mk@hotmail.com
Joseph Russell
Ranch Hand

Joined: Jan 08, 2001
Posts: 290
In regards to Ryan's question. No you don't need to create seperate instance of outerclass so you can comment out the Outerclass o = new Outerclass(); line and it will run just fine.
Thanks Shrini for you definitions.
Asta

[This message has been edited by Joseph Russell (edited February 16, 2001).]
Joseph Russell
Ranch Hand

Joined: Jan 08, 2001
Posts: 290
Why is it the only difference between Top Level Nested Inner Class and Inner Class is that one is static and one is not? Aren't they both nested? It would seem like it would be less wordage to just make it Top Level Inner Class or even just Static Inner Class. Anyone out there no why?
Just curious.
Joe
Shrini Kulkarni
Ranch Hand

Joined: Jan 12, 2001
Posts: 63
Hi,
It is "Static " word that makes this huge difference. An inner class ( non -static) can access all varaibles including private ones of the enclosing class as it has a hidden reference "this" of the enclosing class. But a static inner class has no such privilages. It can acess only static and may be final variables of enclosing class.
Regards
Shrinivas
Joseph Russell
Ranch Hand

Joined: Jan 08, 2001
Posts: 290
I understand what it can do but I was wondering why the name Top Level Nested Inner Class ...however you just called it static inner class. So, now my question is are both of those terms interchangeable. ie Is Top Level Nested Inner Class == static inner class? Terminology is at the heart of programming and I'd like to be clear on the correct useage of both terms. Seems to me a lot easier to say static inner class than top level nested inner class. IMO of course.

Top Level Nested inner class : static inner class - defined just like any other member variable. Declared between open and close braces of the class definition.
Inner class : Refers to non-static inner class - defined just like any other member variable. Declared between open and close braces of the class definition.
posted by Shrini Kulkarni

Joe
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
Could you create a new innerclass of the previously created outerclass object o like so?
Outerclass.Innerclass oi = o.new Innerclass()
Joseph Russell
Ranch Hand

Joined: Jan 08, 2001
Posts: 290
Yeah...you can.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Nested & Inner class