Which statement is true about a static nested class?
2024
Which statement is true about a static nested class?
- A.
You must have a reference to an instance of the enclosing class in order to instantiate it.
- B.
It does not have direct access to non-static members of the enclosing class.
- C.
Its variables and methods must be static.
- D.
It must extend the enclosing class.
Attempted by 6 students.
Show answer & explanation
Correct answer: B
Concept
A nested class marked static is a member of the enclosing class, not of any enclosing instance. Unlike a non-static (inner) class — which implicitly holds a reference to the enclosing object it was created from — a static nested class has no such reference. Only static context (static methods, static blocks, and static nested classes) can access other static members directly; reaching an instance (non-static) field or method from that context requires an explicit object reference, exactly as a static method cannot touch instance members directly.
Application
Take an Outer class with an instance field, say int value, and a static nested class Outer.Nested.
Outer.Nested is created independently of any Outer object — new Outer.Nested() compiles with no enclosing instance in scope, unlike a non-static inner class which needs outerRef.new Inner().
Because no enclosing instance exists inside Nested, code inside it that reads value directly (the instance field) fails to compile — the compiler has nothing to resolve value against.
Accessing it is only possible by taking an explicit Outer reference as a parameter or field inside Nested and reading ref.value through it — confirming the nested class cannot reach non-static members on its own.
Cross-check
This matches the general static-context rule: a static method of Outer also cannot touch value without an explicit Outer object, for the same reason — no implicit enclosing-instance reference exists in a static context. A non-static inner class behaves oppositely, holding that reference automatically.
Why the other statements don't hold
Needing an enclosing-instance reference to instantiate describes a non-static inner class, not a static nested class — a static nested class is instantiated independently of any outer object.
A static nested class is free to declare non-static fields and methods of its own; only its own instances don't carry a link to the enclosing object — the class is not forced to be all-static internally.
Nesting and inheritance are independent features in Java — being declared inside another class carries no extends relationship to it.