This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner class question 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 "inner class question" Watch "inner class question" New topic
Author

inner class question

Chun Wang
Ranch Hand

Joined: Jan 24, 2001
Posts: 42
hello.
I wanna confirm my understanding on inner class. take the following code for example:
public class Outer{
public String name = "Outer";
public static void main(String argv[]){
Inner i = new Outer().new Inner();
i.showName();
}//End of main
private class Inner{
String name =new String("Inner");
void showName(){
System.out.println(name);
}
}//End of Inner class
}
----------------
here reference to Inner object i can be declared by its simple name "Inner" (ie. Inner i = ....) because it's defined in the SAME class as the one the class Inner is defined in. is this correct? is this true for both static and non-static inner class?
if I were to declare a reference to a Inner object from ANOTHER class (say class USER) I would have to say" Outer.Inner i =....", is this correct?
please confirm my understanding. your help is much appreciated.
thanks.
chun
Anshuman Acharya
Ranch Hand

Joined: Jan 19, 2001
Posts: 144
you know all the right answers... you're just not sure...
main is in the same class in which Inner is, so it can see it. it just needs an instance of the Outer alongside to create an instance of Inner.
If main had been called from outside the class, it couldn't have seen Inner. so it has to use Outer.Inner i o refere4nce it. This holds true even if Inner had been static.
The difference between the static and don-static comes in the fact that a static Inner2(say) doesn't need an instance of Outer.
so to create it the following lines would do:
from within the Outer class:
Inner2 i2 = new Inner2();
//Outer.Inner2 i2 = new Outer.Inner2() gives no error... it's just redundant...
//Inner2 i2 = new Outer.new Inner2(); no error , but the i2 created belongs to the class and not one instance of Outer.
from outside:
Outer.Inner2 i2 = new Outer.inner2();
clear?
Steve Aldrich
Greenhorn

Joined: Jan 31, 2001
Posts: 7
main is static. Main can run with no instance of Outer ever existing. Even is Outer were abstract you could still run main.
The construction of
Inner i = new Outer().new Inner(); creates an unnamed reference to Outer, through that instance the constructor of Inner is then called. So, there is an instance of Outer and Inner holds an implicit reference to it. Outer won't get garbage collected until the reference to Inner ( "i" ) is gone.
You can say Inner i = new Outer().new Inner(); instead of Outer.Inner i = new ... because Inner is in scope.
You can't use Inner i = new Inner(); within main because main is static. In main, there is no instance of Outer. There is no "this"
Inner can never be constructed without an instance of Outer, whether that instance of Outer is held by anything or not doesn't matter.
 
 
subject: inner class question
 
Similar Threads
class types
question about inner class instantiation
Marcus green exam 2 : question 32
use of Outer.this
"this "of inner class