• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

inner class very confusing

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
joy
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 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
reply
    Bookmark Topic Watch Topic
  • New Topic