• 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

Inner Class

 
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
How we create new object from non-static inner class and static inner class (nested class) ?
thanks
daniel
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Outer. Inner i = new Outer.Inner();
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Non-static inner class: you need an instance of the outer class:

Static inner classes (top level nested classes): you don't need an instance of the outer class:

HIH
Val
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please could you tell me if these two lines mean the same thing?
Outer.Inner i = new Outer.Inner();// A
Outer.Inner i = new Outer().new Inner(); // B
If they are could you please tell me which is most appropriate version?
Thanks
'Wale

------------------
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, remember:
a)An instance of an inner class needs an instance of the containning class for being created.
b)A non-inner (static) nested class doesn't need it.
c)Within an instance method "this" points to the object over which the invocation was made. Within a constructor this points to the object being constructed.
Now
Outer.Inner i = new Outer.Inner();// A

If Inner was an inner class how would the Inner instance obtain such reference? The only way is if this invocation took place whitin an instance method of the containing class (or its subclasses)or whithing a constructor of the containing class (or its subclasses). Why? Because you are invoking new Outer.Inner() over "this" and only in those contexts "this" is an instance of a class container of Inner.
If Inner was a static nested class A could be placed in any static method of the containning class (or its subclasses)or
other class not being the containning one. But "new Inner()" could be placed only within a static method of the containning class or its subclasses.
While
Outer.Inner i = new Outer().new Inner(); // B
If Inner was an inner class this expression could occur anywhere because you are providing to the instance of Inner the instance of the containning class that needs. But "new Inner()" would compile only if takes place in an instance method or constructor of the containning class (or its subclasses). The reason is because only in these contexts the hidden argument "this" points to an instance of the containning class (or its subclasses).
If Inner was static nested, well, you can invoke a static method using an instance of the class isn't it? This is the same.

hope helps

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic