Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

inner class question  RSS feed

Chun Wang
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}//End of main
private class Inner{
String name =new String("Inner");
void showName(){
}//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.
Anshuman Acharya
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 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();
Steve Aldrich
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!