This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner Class Question

Thomas Markl
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.
Kathy Sierra
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
Marilyn de Queiroz
Posts: 9079
  • 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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!