Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nested & Inner class

 
Shah Chunky
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends...
Can someone tell me what is the difference between Inner Class, Nested Class & Top level nested Class.
Thanks in advance.
 
Joseph Russell
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();"?
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The phrase nested class usually refers to a static inner class.
 
Shrini Kulkarni
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joseph Russell
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah...you can.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic