Difficulty: Easy
Correct Answer: MyOuter.MyInner m = new MyOuter.MyInner();
Explanation:
Introduction / Context:
This question tests your understanding of Java static nested classes (also called static member classes) versus inner classes. Knowing when you need an enclosing instance and how to fully qualify the nested type name is essential for compilation and readability.
Given Data / Assumptions:
Concept / Approach:
A static nested class does not require an instance of the enclosing class. It behaves similarly to a top-level class that is scoped within the outer class's namespace. Therefore, you instantiate it with the fully qualified name and the usual new expression, without using an enclosing object reference.
Step-by-Step Solution:
Verification / Alternative check:
If MyInner were non-static, you would need an instance of MyOuter and the syntax would be: MyOuter outer = new MyOuter(); MyOuter.MyInner mi = outer.new MyInner();
Why Other Options Are Wrong:
Option B and D omit the outer qualifier for the constructor or type; Option C incorrectly tries to use m.new MyOuter.MyInner()
, which is for non-static inner classes; Option E is unqualified and invalid outside the package context without import and still wrong for a nested class.
Common Pitfalls:
Confusing static nested with inner classes and attempting to use the outer.new Inner()
syntax when the nested class is static.
Final Answer:
MyOuter.MyInner m = new MyOuter.MyInner();
Discussion & Comments