• 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 Question

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
that's the question:

public class OuterClass{
class InnerClass{ //instance inner class
}
public void innerClassDemo(){
//Explicit instance of InnerClass
}
}
In above example, how can you explicitly create an instance of InnerClass?
A. InnerClass i=InnerClass();
B. InnerClass i= new OuterClass.InnerClass();
C. InnerClass i=new OuterClass ().new InnerClass();
D. OuterClass.InnerClass i=new OuterClass().new InnerClass();
E. InnerClass i= new InnerClass();
I think correct solution is C, D, E.
E is correct, too because instance of Inner Class is created in a method of the enclosing
Class. Therefore I think it is not neccessary to create an Instance of the outer Class
Because of “this” reference automatically provided.
Is my oppinion regarding E correct???

In the program given above there is an InnerClass Instance a which is created in
Method which is not enclosing class => you must have instance of enclosing class
Created with “new OuterClass()” to create InnerClass instance.
On the contrary there is i which is in enclosing class therefore no explicit Inner class
Neccessary but you could create one?
Ist this correct?
Appreciate your answer.
thomas
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy!
I'm not 100% certain I'm clear about your exact question, but if it has to do with creating an instance of the inner class from within code in the enclosing class, your thinking is correct.
If you are within a context of non-static code from the enclosing class (for example, code running in an instance method of the enclosing class), then yes, as you said, you already HAVE an implicit reference to an instance of the enclosing class ('this'), so you can instantiate an instance of the inner class without making a NEW instance of the outer/enclosing class.
Just remember that for a non-static nested/inner class, you DO need to tie the inner class instance to an instance of the enclosing/outer class. A non-static nested/inner class instance cannot live on its own... it MUST share an intimate connection to an instance of the outer/enclosing class. That special "one inner goes with one outer" connection gives the inner class instance its key capability: accessing all members of the enclosing/outer class.
Cheers,
Kathy

Somewhere on javaranch there's an old, old campfire story called, "Getting in touch with your inner class" that goes into some of the details, and even includes cute pictures
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Getting in Touch with your Inner Class can be found from the Javaranch main page by following the link to Campfire Stories and clicking on the appropriate link. It's a very interesting story that also teaches you a lot about inner classes.
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic