File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Are the following the ONLY ways to instantiate a non static inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Are the following the ONLY ways to instantiate a non static inner class" Watch "Are the following the ONLY ways to instantiate a non static inner class" New topic
Author

Are the following the ONLY ways to instantiate a non static inner class

sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
1. Outer.Inner i = new Outer().new Inner()
2. Outer o = new Outer()
Inner i = o.new Inner()
what about..........
3. Outer o = new Outer() //IS THIS OK ?!!
Outer.Inner i = o.new Inner()
sachin patel
Ranch Hand

Joined: Nov 28, 2000
Posts: 75
looks to me that Option 3 is valid and option 2 is ont valid


Sachin,<P>****************************************************<BR>Learn from others mistakes. Life is too short to make all yourself.<BR>****************************************************
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Depends on where you are trying to create an instance of the inner class. Here are two examples to show my point.
In the first examle, I create the OuterClass and have a main method of the OuterClass that I run.

In this case all three methods work.
For the second example, I have a new class that tries to create an instance of the inner class.

In this case, method 2 will fail since you can no longer referce variables of the class without an instance of the class, so you can't say InnerClass becuase it doesn't know what InnerClass is unless you say o.InnerClass.
So 1 and 3 are always correct and 2 may or may not be correct.
Bill
sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
thanks a lot Bill, that was just brilliant!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Are the following the ONLY ways to instantiate a non static inner class
 
Similar Threads
answers to java.ditmas.net Mock Exam
A class extending inner class
innerclasses
Non static inner classes
Static class