Java
Programming
Coding
Enclosing Class
Computer Science

Java "is not an enclosing class" error: how to create an inner class

Codemia Knowledge Hub ·

The "is not an enclosing class" error, and its close relative "an enclosing instance that contains ... is required", mean you used a non-static inner class without an instance of its outer class, for example new OuterClass.InnerClass() from a static method in another class. An inner class is tied to an instance of its outer class, so create the outer object first and use outer.new InnerClass(), or declare the nested class static if it does not need that instance.

Static nested classes versus inner classes

Java lets you nest classes inside other classes, either static or non-static:

  • Static nested classes belong to the outer class itself. They cannot access the outer class's instance variables.
  • Non-static nested classes (inner classes) are tied to a specific instance of the outer class and can use its instance variables and methods.

When you create a nested class instance, the compiler checks that it is referenced in the right context for its kind.

What triggers the error

  1. Creating an inner class without an outer instance. An inner class must be created through an existing instance of the outer class. Skipping that step causes the error.
  2. Referencing an inner class from a static context. In a static method there is no outer instance available, so the compiler cannot attach one.

This code fails to compile because InnerClass is not static and needs an OuterClass instance. Current javac (JDK 17 and later) reports "an enclosing instance that contains OuterClass.InnerClass is required" here; "not an enclosing class: OuterClass" is what it prints when you write OuterClass.this outside OuterClass:

java
1public class OuterClass {
2    class InnerClass {
3        void display() {
4            System.out.println("Inside Inner Class");
5        }
6    }
7}
8
9public class TestClass {
10    public static void main(String[] args) {
11        OuterClass.InnerClass inner = new OuterClass.InnerClass();
12        inner.display();
13    }
14}

Create the inner class through an outer instance

Create an OuterClass first, then instantiate InnerClass through it:

java
1public class TestClass {
2    public static void main(String[] args) {
3        OuterClass outer = new OuterClass(); // Create an instance of OuterClass
4        OuterClass.InnerClass inner = outer.new InnerClass(); // Properly instantiate InnerClass
5        inner.display();
6    }
7}
IssueFix
Instantiating an inner class without an outer class instanceCreate an instance of the outer class and use it to instantiate the inner class.
Referencing a non-static inner class in a static contextMake the nested class static if it needs to be used from a static context.
  • Instance initialization blocks in an inner class run just before its constructor each time you create an instance.
  • Anonymous inner classes have no named declaration and are handy for implementing interfaces or subclasses on the fly.
  • Local classes are declared inside a method. They behave like inner classes but are not declared at member level.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms