Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner and outer class instances

 
Fawad Khan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sai Ram9
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fawad,

Instance of Enclosing class is a must for creating an instance of Inner Class.
 
Fawad Khan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
---------------------------------------------
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic