aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner and outer class instances Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner and outer class instances" Watch "Inner and outer class instances" New topic
Author

Inner and outer class instances

Fawad Khan
Ranch Hand

Joined: Feb 13, 2001
Posts: 36
Hi,
I have alittle confusion regarding the instances of inner and outer classes.The Book of Robert Heller states
"There must NORMALLY be a pre-existing instances of the outer class acting as context"
while Java Notes say
"An instance of a non-static inner class can exist ONLY with an instance of its enclosing class. So it always has to be created within a context of an outer instance."
So, is it really a case of ONLY or it is a case of NORMALLY.
Thanx in Advance.

Seeking Certification
Sai Ram9
Greenhorn

Joined: Feb 13, 2001
Posts: 18
Fawad,

Instance of Enclosing class is a must for creating an instance of Inner Class.
Fawad Khan
Ranch Hand

Joined: Feb 13, 2001
Posts: 36
But, when i make instance of the inner class without making the instance of the outer class, it compiles fine. So, is this a runtime error only ?
mansoor iqbal
Ranch Hand

Joined: Aug 14, 2000
Posts: 91
Dear Fawad
u r right. following is an example: the line marked with *** compiles ok. but gives a runtime error...the error message is a huge multiline crapola!
is this proof of the fact that u can not instantiate an inner w/o an outer?
or
that the line i added makes no sense!
regards
--------------------------------------
public class OuterOne
{
private int x;
public class InnerOne
{
private int y;
public int z=100;
***InnerOne ione=new InnerOne();// compiles ok, but gives a runtime error
public void innerMethod()
{
System.out.println("enc x is:"+x);
System.out.println("y is:"+y);
}
}
public void outerMethod()
{
System.out.println("x is:"+x);
}
public void makeInner()
{
InnerOne anInner=new InnerOne();
anInner.innerMethod();
}
}
---------------------------------------------
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner and outer class instances