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
- 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.
- 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:
Create the inner class through an outer instance
Create an OuterClass first, then instantiate InnerClass through it:
| Issue | Fix |
| Instantiating an inner class without an outer class instance | Create an instance of the outer class and use it to instantiate the inner class. |
| Referencing a non-static inner class in a static context | Make the nested class static if it needs to be used from a static context. |
Related kinds of inner classes
- 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.
Related reading
- Is null check needed before calling instanceof?
- Is Old Kafka written in Scala and new Kafka written in Java?
- Is Random class thread safe?
- Is shifting bits faster than multiplying and dividing in Java? .NET?
- Is snakeyaml 2.0 added in the new Spring boot versions?
- Is Spring's ThreadPoolTaskExecutor non-blocking?
- Is String a primitive type?
- Is the buildSessionFactory Configuration method deprecated in Hibernate?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.