SCJP Inner Classes and Nested Types — Questions and Answers
Question 1: Which statement about a static nested class is TRUE?
- It can access the instance variables of its enclosing class directly
- It must be instantiated through an instance of the outer class
- It can only contain static members
- It can be instantiated without an instance of the outer class (Correct answer)
Correct answer: It can be instantiated without an instance of the outer class
A static nested class is associated with the outer class itself, not with an instance. You instantiate it as `new Outer.Nested()` without needing an Outer instance. It cannot access instance members of the outer class directly.
Question 2: What is the correct syntax to create an instance of the non-static inner class `Inner` defined inside class `Outer`?
- Inner i = new Inner();
- Outer.Inner i = new Outer.Inner();
- Outer o = new Outer(); Outer.Inner i = o.new Inner(); (Correct answer)
- Outer.Inner i = Outer.new Inner();
Correct answer: Outer o = new Outer(); Outer.Inner i = o.new Inner();
A non-static inner class is tied to an instance of its enclosing class. You must first create an Outer instance, then use the `outerRef.new Inner()` syntax. The other forms are not valid Java.
Question 3: A method-local inner class defined inside an instance method can access which variables from the enclosing method?
- All local variables of the enclosing method
- Only local variables declared final (or effectively final in Java 8+) (Correct answer)
- No variables from the enclosing method
- Only static variables of the enclosing class
Correct answer: Only local variables declared final (or effectively final in Java 8+)
A method-local inner class can access the enclosing class's instance/static members freely, but can only access local variables that are final (Java 6 SCJP rule). This is because the class instance may outlive the method's stack frame.
Question 4: Which of the following is a valid way to create an anonymous class in Java?
- Runnable r = new Runnable() { public void run() {} }; (Correct answer)
- Runnable r = new class() implements Runnable { public void run() {} };
- anonymous Runnable r = new Runnable() { public void run() {} };
- Runnable r = Runnable() { public void run() {} };
Correct answer: Runnable r = new Runnable() { public void run() {} };
Anonymous classes are created with `new InterfaceOrClass() { /* body */ }`. There is no `anonymous` keyword, no `class` keyword in the syntax, and no parentheses-only form without `new`.
Question 5: Can a non-static inner class declare static members?
- Yes, it can declare both static fields and static methods
- Yes, but only static final constant fields (Correct answer)
- No, non-static inner classes cannot have any static members
- Yes, but only static methods, not static fields
Correct answer: Yes, but only static final constant fields
In Java, a non-static inner class may declare static final constant fields (compile-time constants) but cannot declare other static members. This is because the inner class is associated with an outer instance and full static context would be ambiguous.
Question 6: What is the correct way for an inner class to reference the enclosing class's instance variable `x` when the inner class has its own variable `x`?
- super.x
- Outer.this.x (Correct answer)
- outer.x
- this.outer.x
Correct answer: Outer.this.x
`Outer.this` is the qualified `this` reference that refers to the enclosing outer class instance. `super.x` refers to a superclass field, `outer.x` requires an explicit reference variable named `outer`, and `this.outer.x` is not valid Java syntax.
Which statement about a static nested class is TRUE?