Multiple choice technology

public class Outer { public void someOuterMethod() { //Line 5 } public class Inner { } public static void main(String[] argv) { Outer ot = new Outer(); //Line 10 } } Which of the following code fragments inserted, will allow to compile? A. new Inner(); //At line 5 B. new Inner(); //At line 10 C. new ot.Inner(); //At line 10 D. new Outer.Inner(); //At line 10

  1. A

  2. B

  3. C

  4. D

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

Inner is a non-static inner class, so it requires an instance of Outer to be instantiated. At line 5, we're inside an instance method (someOuterMethod), so the implicit 'this' refers to an Outer instance; 'new Inner()' (A) works. At line 10, we're in a static context (main method) with no Outer instance available, so 'new Inner()' (B) fails. Option C 'new ot.Inner()' is invalid syntax: should be 'ot.new Inner()'. Option D 'new Outer.Inner()' would work if Inner were static.