This sample program check your knowledge of Inner class and outer class in Java.
This sample program check your knowledge of Inner class and outer class in Java.Given a sample code:
1 public class Test {
2 public static void main(String[] args) {
3 OuterClass.InnerClass i = new OuterClass.new InnerClass();
4 i.innerMethod();
5 OuterClass o = new OuterClass();
6 o.x=5;
}}
7 class OuterClass {
8 public int x = 9;
9 public OuterClass() {
10 InnerClass inner = new InnerClass();
11 inner.innerMethod();
}
12 class InnerClass {
13 public void innerMethod() {
14 System.out.print(x);
}}}
What will be the result of above code ?
(1) Prints 555
(2) Error during compilation at line 6
(3) Error during compilation at line 11
(4) Prints 999
(4)