• 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Himai Minh

inner class question

Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 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();
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Hang a left on main. Then read this tiny ad:
Garden Master Course kickstarter
    Bookmark Topic Watch Topic
  • New Topic