• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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
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
 
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();
clear?
 
Greenhorn
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.
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic