wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Circularity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Circularity" Watch "Circularity" New topic
Author

Circularity

mohit joshi
Ranch Hand

Joined: Sep 23, 2000
Posts: 243
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

Joined: Sep 16, 2000
Posts: 100
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

Joined: Sep 23, 2000
Posts: 243
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

Joined: Aug 30, 2000
Posts: 3141
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).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
djroberts
Greenhorn

Joined: Sep 20, 2000
Posts: 6
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

Joined: Aug 30, 2000
Posts: 3141
Thanks dj. That's what I thought too but wasn't sure.
Jane
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Circularity
 
Similar Threads
Inner Class/Overridding
this and super
One more doubt
Tricky Questions(Can anybody guide me)
gc