inner class very confusing
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi!
This code compiles don't know why.
class D
{ int a = 1;
class Inner{
int a = 2;
void printme(){ System.out.println(this.a);
System.out.println(D.this.a);}
}
public static void main(String args[]){
Inner c = new D().new Inner();
c.printme();
}
}
This program compiles and prints 2 and 1;
the instantiation of Inner c works fine.How can that be? I thought that a non-static inner class should always be referenced with the instance of the enclosing class? why is this working?
Please help.
regds,
joy
This code compiles don't know why.
class D
{ int a = 1;
class Inner{
int a = 2;
void printme(){ System.out.println(this.a);
System.out.println(D.this.a);}
}
public static void main(String args[]){
Inner c = new D().new Inner();
c.printme();
}
}
This program compiles and prints 2 and 1;
the instantiation of Inner c works fine.How can that be? I thought that a non-static inner class should always be referenced with the instance of the enclosing class? why is this working?
Please help.
regds,
joy
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Joy,
You are right, the non-static inner class always needs an instance of the enclosing class. That's why you are first instantiating D, which is the enclosing class.
The statement Inner c = new D().new Inner(); first instantiates an object of type D and then creates the Inner class object with that instance. Also, because the main method is with in the scope of the outer class D, the Inner class name need not be qualified with the name of the outer class.
Does this answer your question?
Ajith
You are right, the non-static inner class always needs an instance of the enclosing class. That's why you are first instantiating D, which is the enclosing class.
The statement Inner c = new D().new Inner(); first instantiates an object of type D and then creates the Inner class object with that instance. Also, because the main method is with in the scope of the outer class D, the Inner class name need not be qualified with the name of the outer class.
Does this answer your question?
Ajith
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
joy
Greenhorn
Posts: 16
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
ajith,
I understand if Inner class is a static class but its not.
doesn't class Inner work like any member of the outer class D?. Why can I access it from the main method when the Inner class is non static?
class D{
int i;
class Inner{
}
public statuc void main(String args[])
i=0;//won't work bcoz i is not static
Inner c //will work.its also non-static?
}
regds,
joy
I understand if Inner class is a static class but its not.
doesn't class Inner work like any member of the outer class D?. Why can I access it from the main method when the Inner class is non static?
class D{
int i;
class Inner{
}
public statuc void main(String args[])
i=0;//won't work bcoz i is not static
Inner c //will work.its also non-static?
}
regds,
joy
Ajith Kallambella
Sheriff
Posts: 5782
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Joy,
I don't think I got your question right.
If you are asking how you are able to define the "Inner c" reference in the main method, it is due to visibility. Because Inner is a part of the same class D that the main method belongs to, the Inner class is visible. However, since the Inner class is non-static, you cannot say Inner.printme().
In your second code snippet, you are only declaring a variable of type Inner. That's perfectly valid.
Does this answer your question?
Ajith
I don't think I got your question right.

If you are asking how you are able to define the "Inner c" reference in the main method, it is due to visibility. Because Inner is a part of the same class D that the main method belongs to, the Inner class is visible. However, since the Inner class is non-static, you cannot say Inner.printme().
In your second code snippet, you are only declaring a variable of type Inner. That's perfectly valid.
Does this answer your question?
Ajith
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
| I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |






