Difficulty: Easy
Correct Answer: Foo.Bar b = f.new Bar();
Explanation:
Introduction / Context:This tests knowledge of instantiating a non-static member inner class. Such a class requires an instance of the outer class to create the inner instance, and its type name must be qualified with the outer class name.
Given Data / Assumptions:
Concept / Approach:Syntax for member inner class instantiation is: OuterClass.InnerClass var = outerInstance.new InnerClass(); The unqualified InnerClass name is not visible at top level; you must use the qualified type.
Step-by-Step Solution:
Use Foo.Bar as the type on the left-hand side.Use f.new Bar() to bind the new inner instance to the existing Foo instance f.Therefore: Foo.Bar b = f.new Bar();Verification / Alternative check:Trying new Foo.Bar() without an instance fails because Bar is not static; trying Bar b = f.new Bar() fails because Bar is not in scope without Foo qualification.
Why Other Options Are Wrong:
Common Pitfalls:Forgetting the outer instance requirement and the qualified type name for non-static inner classes.
Final Answer:Foo.Bar b = f.new Bar();
Discussion & Comments