How do you instantiate an inner (non-static) class from outside its enclosing class? Explain the outer.new Inner() syntax.
answer
- outer.new Inner() supplies the enclosing instance
- type is Outer.Inner
- new Outer.Inner() is illegal
- inside outer non-static method: plain new Inner()
- static nested uses new Outer.Nested()
basics
~20 sYou first need an outer-class object, then create the inner object from it: Outer outer = new Outer(); Outer.Inner inner = outer.new Inner();. You can't write new Outer.Inner() because every inner instance needs an enclosing instance.
solid answer
~40 sBecause a non-static inner instance is bound to an enclosing instance, you cannot construct it standalone — you must construct it *from* an outer object. The syntax is `outerInstance.new Inner(args)`: the qualifying expression before `.new` supplies the enclosing instance that the new inner object will reference. The type of the result is `Outer.Inner`. Inside the outer class's own non-static methods you rarely write this because plain `new Inner()` implicitly uses the current `this` as the enclosing instance. The forms `new Outer.Inner()` and `new Inner()` from a static context are compile errors, since neither supplies an enclosing instance. A static nested class is the opposite: it is built with `new Outer.Nested()` and needs no outer instance.
code
java · 16 linesclass Outer {
class Inner {
Inner() { System.out.println("inner built"); }
}
Inner make() {
return new Inner(); // implicit this.new Inner()
}
public static void main(String[] args) {
Outer outer = new Outer();
Outer.Inner a = outer.new Inner(); // explicit, from outside
Outer.Inner b = outer.make(); // built inside Outer
// Outer.Inner c = new Outer.Inner(); // COMPILE ERROR: no enclosing instance
}
}go deeper
Recognizes that an inner class needs an outer object and has seen the outer.new Inner() syntax.
Writes the qualified-new form correctly, knows the Outer.Inner type name, and knows the implicit-this shortcut inside the outer class.
Explains exactly why the standalone forms fail (no enclosing instance) and contrasts cleanly with static-nested construction.
Considers API ergonomics — e.g., exposing a factory method instead of forcing callers to use outer.new, and the lifetime implications of handing out inner instances.
## Why special syntax exists A **non-static inner class** instance always carries a hidden reference to an **enclosing (outer) instance**. So to make one, the compiler must know *which* outer object it attaches to. Ordinary `new Type()` provides no outer object, which is why a dedicated syntax exists. ## The qualified `new` form From outside the outer class: ```java Outer outer = new Outer(); // 1. need an outer instance first Outer.Inner inner = outer.new Inner(); // 2. create inner FROM it ``` Read `outer.new Inner()` as: "using `outer` as the enclosing instance, make a new `Inner`." The expression to the left of `.new` is the enclosing instance. The declared type is the qualified name **`Outer.Inner`**. Constructor arguments go in the parentheses as usual: `outer.new Inner(42)`. ## From inside the outer class Within a **non-static** method of `Outer`, you can write plain `new Inner()`. The current object (`this`) is silently used as the enclosing instance — equivalent to `this.new Inner()`. This is why most code never shows the `outer.new` form: it usually happens inside the outer class. ## What is illegal and why - `new Outer.Inner()` — **illegal**: looks like it builds an inner class but supplies no enclosing instance. - `new Inner()` inside a **static** method of `Outer` — **illegal**: a static context has no `this`, so there is no enclosing instance to attach. The compiler error is typically *"an enclosing instance that contains Outer.Inner is required."* ## Contrast with static nested classes A **static nested class** has no enclosing instance, so it is created with `new Outer.Nested()` and works in any context. This mirror-image construction is a frequent interview discriminator. ## Mental checklist 1. Need an inner instance? → first get/recall an outer instance. 2. Outside the outer class → `outerRef.new Inner(...)`. 3. Inside a non-static outer method → `new Inner(...)` (implicit `this`). 4. Static context → you must explicitly provide an outer instance via the qualified form.
- How does instantiating a static nested class differ syntactically?A static nested class needs no enclosing instance, so you write `new Outer.Nested()` directly — no preceding outer object. The qualified `outer.new` form is only for non-static inner classes.
saying these in an interview costs you the question
- Writing `new Outer.Inner()` and expecting it to compile
- Forgetting you need an outer instance at all
- Trying `new Inner()` from a static method without an enclosing instance