Can someone explain whether the following code will compile or not. public class Test009 { public static void main(String args[]) { Test009.Inner inner1=new Test009().new Inner(); //1 Inner inner2=new Test009().new Inner(); //2 inner2.method(); //3 } class Inner { private void method() { System.out.println("0"); } } }
It compiles and gives output 0 Non-static inner class are pretty much dependent on the outer class. They cannot exist without the instance of outerclass
If thats the case why is there a difference between statement 1 & 2 i.e the in the inner class variable type declaration.
Originally posted by Ragu Sivaraman: It compiles and gives output 0 Non-static inner class are pretty much dependent on the outer class. They cannot exist without the instance of outerclass
Well, to create a reference of the type of the Inner class, you only need to specify the name of the Inner, 'if you are into the Outer class': Inner inner; But if you were outside the Outer class, you will need the long form: Test009.Inner inner;
midirectorio, Please read the JavaRanch Name Policy and re-register using a name that complies with the rules. Thanks for your cooperation. ------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform