"Objects that are instances of an inner class exist within an instance of the outer class"
Even though I attempt to create an independent instantiation of the inner class, the outer class is always there because of the statement above.

Is that ture?

If true, then that would also mean the inner class holds a reference to the outer class. With the anonymous outer class, it (outer) would "live" until the inner class goes out of scope.

Yes?
AirJet.java
public class AirJet {
   public int model = 777;
   class Engine{
       public Engine() {
          model = 747;
       }
       public int getModel(){
          return model;
       }
   }
}
AirJetDemo.java
public class AirJetDemo {
   public static void main(String[] args) {
       // instantiation 1
       AirJet.Engine engine = new AirJet().new Engine();
       System.out.println("Model: " + engine.getModel());
  }
}

Dani AI

Generated

is right: a non‑static inner class instance is tied to a specific outer instance. The Java compiler implements that tie by adding an implicit (synthetic) field in the inner class that points to the outer object (often seen as a field named something like this$0 in bytecode/heap dumps). That implicit reference is why the outer instance cannot be garbage collected while the inner is still reachable.

Practical implications: if an inner instance is stored in a long‑lived place (a static field, a cache, a background thread, etc.) you may unintentionally keep the outer alive and cause a memory leak. Serialization of an inner instance will also pull the outer along unless you take steps to avoid it. To break the implicit link, make the nested type static or move it to top level so it does not get the synthetic outer reference.

A minimal pattern showing a static nested class (no implicit outer reference):

class Outer {
  static class Nested {
    // no implicit reference to Outer
  }
}

Outer.Nested n = new Outer.Nested();

If you must keep the class nested but avoid strong retention of the outer, pass only the data the nested type needs (or pass a WeakReference to the outer). For debugging, a heap dump and a tool like a Java heap analyzer will show inner objects holding this$0 references — that quickly reveals which inner object is keeping a larger outer object alive. Overall, use a static nested or top‑level class when you want an independent instance; use a non‑static inner only when you truly need direct access to the specific outer instance.

Recommended Answers

All 2 Replies

First a point of terminology: there's no such thing as an "anonymous outer class". Only inner classes can be anonymous.
Anyway, "yes" to both your questions. You have to create an instance of the outer class before you can create an instance of the inner class (unless the inner class is static, of course). An instance of an inner class has a reference to the outer class which it uses to access the members of the outer class, you can use that reference explicitly eg Airjet.this.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.