File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Circularity

 
mohit joshi
Ranch Hand
Posts: 243
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what would be the printout when you run the following code?
public class Outer2
{ public class Inner extends Outer2{
public Inner(){
System.out.println("inner constructor");
}
}
public Outer2(){
System.out.println("outer constructor");
Inner inn = new Inner();
}
public static void main(String[] args)
{
Outer2 out = new Outer2();
}
}
If an inner class within and outer class has another private inner class, then can the outer Class create Instances of the innermost Class?
[This message has been edited by mohit joshi (edited October 02, 2000).]
[This message has been edited by mohit joshi (edited October 02, 2000).]
 
Ramesh Donnipadu
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the innermost class is declared "private", you can not create its instance from the outermost class. However you can create any number of instances of innermost class in innerclass
 
mohit joshi
Ranch Hand
Posts: 243
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the compiler doesnt object to it. The following code compiles well ( sorry for the huge code ).
Inner inn=new Inner();
private class Inner{
String str="bc";
public Inner(){
System.out.println("inner constructor");
}
void prnt(){
System.out.println("inner method");
}
private class Inner2 extends Inner
{ //String str="c";
public Inner2(){
// System.out.println("inner2 constructor"+str);
System.out.println("inner2 constructor"+this.str);
System.out.println("inner2 constructor"+Outer.this.str);
}
void prnt(){
super.prnt();
System.out.println("inner2 method");
}
}

} //end of inner
public Outer(){
inn.prnt();
Inner.Inner2 inn2 = new Inner().new Inner2();
inn2.prnt();
}
void prnt(){
System.out.println("outer method");
}
public static void main(String[] args)
{
Outer out = new Outer();
}

}
 
Jane Griscti
Ranch Hand
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow!! Mohit, where did you get the code for the first example? When I compiled it under JDK 1.3 it continually spewed out the line "outer constructor" until I did a CTRL-BREAK!
When I changed
<code>public Inner extends Outer2 {</code>
to
<code>public Inner { </code>
the output was:
<pre>
outer constructor
inner constructor
</pre>
Can anyone explain why the original code looped continually? Does it have something to do with the superclass ctor's being implicitly called?
Thanks,
Jane
[This message has been edited by Jane Griscti (edited October 02, 2000).]
 
djroberts
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It loops becuase the out constructor creates and inner, when inner is created it executes it's superclasses constructor which intern create another inner, and so on.
I think
- David
 
Jane Griscti
Ranch Hand
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks dj. That's what I thought too but wasn't sure.
Jane
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic