| Author |
Marcus mock exam 2 (quest. #32)
|
Mansi Vyas
Greenhorn
Joined: Jul 01, 2003
Posts: 27
|
|
I'm confused about the inner class being declared private. Here's the question: What will happen when you attempt to compile and run this program: public class Outer { public String name = "Outer"; public static void main(String argv[]) { Inner i = 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 } 1) Compile and run with output of "Outer" 2) Compile and run with output of "Inner" 3) Compile time error because Inner is declared as private 4) Compile time error because of the line creating the instance of Inner The correct answer is option 4. I thought that the program would also generate a compile time error because Inner is declared as private, but it seems to work fine. Why is this the case? Pls help. Thanks. Mansi
|
 |
Cathy Song
Ranch Hand
Joined: Aug 24, 2003
Posts: 270
|
|
Hi Mansi, main() method is static and can access oONLY static methods and variables directly. For the given program, you can have to create an instance of the Outer class to access the inner class. Inner i = new Outer().new Inner(); But, if the Inner class was static, then main() could directly create an instance of the Inner class. private static class Inner { .. } Hope this helps;-)
|
 |
Sizhnor Rhena
Greenhorn
Joined: Nov 17, 2003
Posts: 4
|
|
Here is the error message I got when I compile Outer.java:5: non-static variable this cannot be referenced from a static context Inner i = new Inner(); ^ 1 error in order to make this code work we need to add static in the inner class then only static void main method can access inner class. static only acces static! here is working code! public class Outer { public String name = "Outer"; public static void main(String argv[]) { Inner i = new Inner(); i.showName(); }//End of main private static class Inner { String name =new String("Inner"); void showName() { System.out.println(name); } }//End of Inner class }
|
 |
Sagarika nair
Ranch Hand
Joined: Aug 13, 2003
Posts: 39
|
|
Hi ! This is regarding your question on inner classes. public class Outer { public String name = "Outer"; public static void main(String argv[]) { Inner i = 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 } The program would give a compiler error on the line Inner i= new Inner() because this is possible only if the instantiation is done from inside the outer class instance code. But here the instantiation is done within the main method which is static and so the correct way of instantiating it would be Outer.Inner i=new Outer().new Inner(); So the rule is From outside the outer class instance code(including static method code within the outer class), the inner class name must include the outer class name (Outer.Inner) and to instantiate ,you must use a reference to the outer class(i.e new Outer().new Inner() ) Inner classes can be marked private and it becomes a private member of the outer class.The code would work fine if the inner class was made static b'cos main can access static members directly. This is the explanation according to the K&B book.Hope I made myself clear and if it's wrong please correct me. Good luck. [ November 18, 2003: Message edited by: Sagarika nair ]
|
 |
 |
|
|
subject: Marcus mock exam 2 (quest. #32)
|
|
|