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

inner class

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class outer
{
protected class inner
{
public inner()
{ System.out.println("in inner");
}
}
private inner p1;
outer()
{System.out.println("in constructor of outer class");
p1=new inner();//1
}
public static void main(String a[])
{ new outer(); }
}
AT line 1 i.e even before an object of outer class is created
inner class object is created.How is it working??? since
innerclass can be created after outer class object is created.
Thanks!
 
ricky gonzalez
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does an object of an inner class have to be created after an object of an outer class is created? Actually, you can co-create them at the same time. ie. new Outer.Inner();, which is similar to your case I think.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Rickey.The inner class cannot exist on its own.It is
always binded with the outer class.So,in ur code
it should be new outer().new inner().Please correct me
where i am wrong.
Thanks
 
Amar Srivastava
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually one could create inner classes in both the ways
1) to create STATIC inner classes: new Outer.Inner()
2) otherwise: new outer().new inner()
The position of the code DOES not determine when it would be executed. JAVA has the entry point at function "main", at main you called new outer() so the ctor for outer would be called and then the code for inner's ctor would be called which is perfectly consistent with the way outer and inner are supposed to work.
[This message has been edited by Amar Srivastava (edited July 26, 2000).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic