• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Inner Class Question  RSS feed

Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Cowgirl and Author
Posts: 1589
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

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
Posts: 9099
  • Mark post as helpful
  • send pies
  • 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 is the word "abbreviation" so long? And this ad is so short?
ScroogeXHTML - small and flexible RTF to HTML converter library
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!