What causes error No enclosing instance of type Foo is accessible and how do I fix it?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
When working with Java programming, you may encounter the error message "No enclosing instance of type Foo is accessible." This error typically appears in scenarios involving inner classes where the inner class is not correctly instantiated relative to its outer class context. Understanding the structure of nested classes in Java and the context in which they are used helps in resolving this issue.
Understanding Inner Classes
In Java, it's possible to nest a class within another class. These nested classes are known as inner classes, and they are associated with an instance of the outer class. Inner classes can access the fields and methods of the outer class directly, making them useful for handling complex data structures where one class is tightly coupled with another.
There are four types of nested classes in Java:
- Non-static nested class (Inner class) - associated with an instance of its enclosing class and can access its enclosing instance variables and methods.
- Static nested class - not associated with a specific instance and can only access the static members of the outer class.
- Local class - class defined within a block, typically within a method.
- Anonymous class - a local class without a name.
The error in question most often involves non-static inner classes.
Cause of the Error
The error "No enclosing instance of type Foo is accessible" occurs when you try to instantiate an inner class in a context where the outer class does not exist. Since every instance of the inner class needs to be associated with an instance of the outer class, attempting to instantiate it without such a context leads to this compiler error.
Example Scenario
Consider the following Java code:
This will result in the error "No enclosing instance of type Outer is accessible" because Inner is not a static class, and we're trying to instantiate it without having an instance of Outer.
Resolving the Error
To resolve this error, you need to ensure that you instantiate the inner class only within the context of an existing outer class instance. The correction to the previous example would be:
Alternatively, if the design permits, you can change the inner class to a static nested class so that it does not require an instance of the outer class:
Summary Table
| Issue Description | Solution |
| Instantiating an inner class without a context of an outer class instance. | Create an instance of the outer class, and create the inner class from that instance. |
| Alternative solution | Convert the inner class to a static nested class if it does not need access to an instance of the outer class. |
Conclusion
Understanding the relationship between nested classes in Java is essential for organizing complex class structures and avoiding runtime and compiler errors. When dealing with non-static inner classes, always ensure they are instantiated within the correct context of their enclosing outer class. With these considerations in mind, you can effectively manage and utilize nested classes in your Java applications.
Related reading
- What causes imported Maven project in Eclipse to use Java 1.5 instead of Java 1.6 by default and how can I ensure it doesn't?
- What causes javac to issue the uses unchecked or unsafe operations warning
- What causes java.lang.IncompatibleClassChangeError?
- What causes Unable to access jarfile error?
- What causes unknown resolver null in Spark Kafka Connector?
- What causing this Invalid length for a Base-64 char array
- What could be the cause of RejectedExecutionException
- What could cause java.lang.reflect.InvocationTargetException?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.