Nested and inner classes

A class may be declared inside another class. The nested class is a member of the outer one, its name is qualified by the outer name, and it can be static, an inner class with a reference to an outer object, or anonymous, written at the point where it is used.

A static nested class

A static nested class is a class like any other that happens to live inside another file and name. It has no outer object, it is created with new Outer.Nested(), and it can be used without ever creating the outer class. The nesting is for organisation and for access to the private members of the outer class.

The file Outer.java also has an inner class and an anonymous one:

public class Outer { private int value = 7; static class Nested { String name() { return "static nested"; } } class Inner { int value() { return value; } } public static void main(String[] args) { Outer.Nested nested = new Outer.Nested(); System.out.println(nested.name()); Outer outer = new Outer(); Outer.Inner inner = outer.new Inner(); System.out.println("inner sees: " + inner.value()); Runnable task = new Runnable() { @Override public void run() { System.out.println("anonymous"); } }; task.run(); } }

javac -Xlint:all Outer.java
java Outer

static nested
inner sees: 7
anonymous

The first line needs no Outer object. The second and third show the two other forms, which the rest of the lesson takes apart.

An inner class

An inner class has no static, so every instance of it belongs to one instance of the outer class. It reads the outer fields directly, as value() reads value, and it is created with outer.new Inner(), which names the outer object it belongs to.

That link is the difference from a static nested class: an inner object cannot exist without an outer one, and it keeps the outer one alive as long as the inner one is reachable. A long-lived inner object can therefore hold a whole outer object in memory, which is the usual reason to prefer a static nested class when the link is not needed.

An anonymous class

new Runnable() { ... } declares a class with no name, implements the interface and creates one object of it. The class cannot be named again, so it is for a one-off implementation, and it can read the effectively final locals of the method around it.

Since Java 8 a lambda replaces an anonymous class that implements a functional interface, so Runnable task = () -> System.out.println("anonymous"); is the shorter form of the same object. The anonymous class is still the tool for an interface with two methods or for a class that must extend another class.

When each one fits

A static nested class is the default choice: it groups a helper type with the class that uses it and needs nothing from an outer object. An inner class is for a view or an iterator that is meaningful only in the context of one outer object. An anonymous class is for an implementation used exactly once at the point where it is written.

A local class, declared inside a method, is the fourth form and the rarest; it is visible only in that method. A nested interface is implicitly static, and an enum nested in a class is static as well.

Exercises

  1. Add a static method to Outer.Nested and call it without an outer object.
  2. Add a field to Inner and a second outer object, and check that each inner belongs to its own outer.
  3. Replace the anonymous Runnable with a lambda and run the program again.
  4. Move Nested out of Outer into its own file and adjust the names.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. How is a static nested class created?

    Answer

    new Outer.Nested()

    It has no outer object, so the outer name is only part of the class name.

  2. What does an inner class have that a static nested class does not?

    Answer

    A reference to an outer object

    An inner instance belongs to one outer instance and can read its fields.

  3. What is an anonymous class for?

    Answer

    A one-off implementation written where it is used

    It has no name to refer to, so it is created once at the point of use.

  4. What does this program print? public class Q1 { static class Helper { String text() { return "helper"; } } public static void main(String[] args) { Q1.Helper helper = new Q1.Helper(); System.out.println(helper.text()); } }

    Answer

    helper

    The static nested class is created without an outer object and its method returns helper.

  5. What does this program print? public class Q2 { private int value = 5; class Inner { int value() { return value; } } public static void main(String[] args) { Q2 outer = new Q2(); Q2.Inner inner = outer.new Inner(); System.out.println(inner.value()); } }

    Answer

    5

    The inner object reads the field of the outer object it belongs to.

  6. What does this program print? public class Q3 { public static void main(String[] args) { Runnable task = new Runnable() { @Override public void run() { System.out.println("run"); } }; task.run(); } }

    Answer

    run

    The anonymous class implements run, and the call executes it.

  7. What does this program print? public class Q4 { static class Counter { static int count; static void add() { count++; } } public static void main(String[] args) { Counter.add(); Counter.add(); System.out.println(Counter.count); } }

    Answer

    2

    A static nested class can have static members, and two calls increment the count.

  8. Which form can be replaced by a lambda?

    Answer

    An anonymous class that implements a functional interface

    A lambda is the short form of a one-method interface implementation.

  9. Why can an inner object keep a whole outer object in memory?

    Answer

    It holds a reference to its outer instance

    The link to the outer object keeps it reachable for as long as the inner object lives.

  10. What does this program print? public class Q5 { public static void main(String[] args) { int base = 10; class Local { int add(int n) { return base + n; } } System.out.println(new Local().add(5)); } }

    Answer

    15

    The local class reads the effectively final local base and adds 5.

The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.

Article author: Arthur Isaev

Banner image