Given:
3. public class Main { 4. private int x = 2; 5. protected int y = 3; 6. private static int m1 = 4; 7. protected static int m2 = 5; 8. public static void main(String[] args) { 9. int x = 6; int y = 7; 10. int m1 = 8; int m2 = 9; 11. new Main().new Secret().go(); 12. } //from w ww . ja v a2s .com 13. class Secret { 14. void go() { System.out.println(x + " " + y + " " + m1 + " " + m2); } 15. } }
What is the result?
A is correct.
All of the code is legal.
The inner class has access to all of the outer class's variables (even the private ones), except for main()
's local variables.
Remember that an inner class must have an instance of the outer class to be tied to, which is why line 11 starts by creating an instance of Main.