File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Inner Class Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inner Class Question" Watch "Inner Class Question" New topic

Inner Class Question

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
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

Joined: Oct 10, 2002
Posts: 1589
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

Co-Author of Head First Design Patterns
Just a Jini girl living in a J2EE world.
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
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.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
I agree. Here's the link:
subject: Inner Class Question
jQuery in Action, 3rd edition