Java OCA OCP Practice Question 2352

Question

Which of the classes, when inserted at //INSERT CODE HERE, will create an instance of class Inner? (Choose all that apply.)

class Outer {
    class Inner {}
}
class Test {
    //INSERT CODE HERE
}
a  Outer.Inner inner = new Outer.Inner();
b  Outer.Inner inner = Outer().new Inner();
c  Outer.Inner inner = Outer.new Inner();
d  Outer.Inner inner = new Outer().new Inner();
e  Outer.Inner inner = new Inner();
f  Outer outer = new Outer();
   Inner inner = new Outer.Inner();
g  Outer outer = new Outer();
   Outer.Inner inner = outer.new Inner();
h  Outer outer = new Outer();
   Inner inner = new outer.Inner();


d, g

Note

An inner class is a member of its outer class and can't exist without its instance.

To create an instance of an inner class outside either the outer or inner class, you must do the following:.

  • If using a reference variable, use type Outer.Inner.
  • Access an instance of the Outer class.
  • Create an instance of the Inner class.

To create instances of both the Outer and Inner classes on a single line, you must use the operator new with both the Outer class and Inner class, as follows:

new Outer().new Inner();

If you already have access to an instance of Outer class, say, outer, call new Inner() by using outer:.

outer.new Inner();



PreviousNext

Related