Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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
Author

Inner Class Question

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
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
Kathy Sierra
Cowgirl and Author
Ranch Hand

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


Co-Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0596007124/ref=jranch-20" target="_blank" rel="nofollow">"Head First Design Patterns"</a><br /> <br />Just a Jini girl living in a J2EE world.
Marilyn de Queiroz
Sheriff

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


JavaBeginnersFaq
"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: http://aspose.com/file-tools
 
subject: Inner Class Question
 
Similar Threads
Certification4Career SCJP Mock Exam Errors??
Inner Class
inner class instantiation
Declaration of inner class
Are the following the ONLY ways to instantiate a non static inner class