Share on Google+Share on Google+

SCJP Module-3 Question-2

Advertisement
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

Answer:

(4)

Advertisements

Posted on: July 9, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.