How to access outer class from an inner class?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Java, a non-static inner class holds an implicit reference to its enclosing (outer) class instance. You access the outer class using OuterClass.this. Static nested classes do not have this reference and require an explicit parameter. Python and C# handle inner-to-outer access differently — Python has no implicit reference, and C# only supports static nested types.
Java: Non-Static Inner Class
A non-static inner class automatically gets a reference to the outer class instance that created it:
Outer.this is the syntax for accessing the enclosing instance. Without name conflicts, you can access outer fields directly.
Java: Static Nested Class
Static nested classes have no implicit outer reference:
Static nested classes are just regular classes scoped inside another class. Use them when the inner class does not need the outer instance.
Java: Passing Outer Reference Explicitly
When you need outer access from a static context or want explicit control:
Explicit passing is preferred in modern Java — it makes the dependency clear and avoids the hidden reference.
Java: Anonymous Inner Classes
Anonymous inner classes also capture the outer instance:
Lambdas (Java 8+) also capture the enclosing instance but do not create a separate this:
Python
Python classes do not have an implicit outer reference. Pass it explicitly:
Python's inner classes are just classes defined inside another class's scope. There is no Outer.this equivalent — you must pass the reference manually.
C#
C# only supports static nested classes (no implicit outer reference):
Kotlin
Kotlin uses this@Outer syntax (inner classes must be marked with the inner keyword):
Without the inner keyword, Kotlin nested classes are static by default (no outer reference).
Common Pitfalls
- Memory leaks from inner class references: Non-static inner classes hold a strong reference to the outer instance. In Android, an inner
AsyncTaskorHandlerin an Activity prevents the Activity from being garbage collected. Use static nested classes withWeakReferenceinstead. - Serialization issues: Serializing a non-static inner class also serializes the outer instance (because of the implicit reference). This can fail or produce unexpectedly large serialized objects.
- Confusing
thisin anonymous classes: Inside an anonymous inner class,thisrefers to the anonymous class, not the enclosing class. UseOuterClass.thisto access the enclosing instance. - Assuming Python has implicit outer access: Python nested classes have no implicit reference to the enclosing instance. Always pass the outer reference as a constructor parameter.
- Using non-static when static suffices: If the inner class does not need the outer instance, make it static (
static class Innerin Java, removeinnerin Kotlin). This avoids unnecessary memory overhead and potential leaks.
Summary
- Java non-static inner classes access the outer instance via
OuterClass.this - Java static nested classes have no outer reference — pass it explicitly if needed
- Python has no implicit outer reference — always pass the enclosing instance manually
- C# only supports the equivalent of Java's static nested classes
- Kotlin uses
this@Outersyntax and requires theinnerkeyword for non-static inner classes - Prefer static nested classes to avoid memory leaks and hidden dependencies
Related reading
- How to access Spring-boot JMX remotely
- How to acknowledge current offset in spring kafka for manual commit
- How to activate JMX on my JVM for access with jconsole?
- How to add a custom health check in spring boot health?
- How to add a dependency to a Spring Boot Jar in another project?
- How to add a jar in External Libraries in Android Studio?
- How to add an image to a JPanel?
- How to add Cache-Control header to static resource in Spring Boot?

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.