Review Questions

7.1 What will be the result of attempting to compile and run the following code?
public class MyClass {
    public static void main(String[] args) {
        Outer objRef = new Outer();
        System.out.println(objRef.createInner().getSecret());
    }
}

class Outer {
    private int secret;
    Outer() { secret = 123; }

    class Inner {
        int getSecret() { return secret; }
    }

    Inner createInner() { return new Inner(); }
}

Select the one correct answer.

  1. The code will fail to compile because the class Inner cannot be declared within the class Outer.

  2. The code will fail to compile because the method createInner() cannot be allowed to pass objects of the class Inner to ...

Get Programmer's Guide to Java™ Certification, A: A Comprehensive Primer, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.