How can I access a field of an outer class from a nested class?
How to Access a Field of an Outer Class from a Nested Class in Java
A nested class (inner class or static nested class) can access the fields of its outer class in different ways, depending on whether the class is static or non-static.
1. Accessing Outer Class Fields from a Non-Static (Inner) Class
A non-static inner class has a direct reference to the outer class instance, allowing it to access all fields (even private ones).
Example: Accessing an Outer Class Field from an Inner Class
class Outer {
private String message = "Hello from Outer!";
class Inner {
void display() {
System.out.println("Outer message: " + message); // ✅ Direct access to outer field
}
}
void createInner() {
Inner inner = new Inner();
inner.display();
}
}
public class Main {
public static void main(String[] args) {
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner(); // Creating inner class instance
inner.display();
}
}
Output
Outer message: Hello from Outer!
✅ Inner class has direct access to all members of the outer class.
2. Accessing Outer Class Fields from a Static Nested Class
A static nested class does not have a reference to an instance of the outer class, so it can only access static fields of the outer class.
Example: Accessing Static Fields from a Static Nested Class
class Outer {
private static String staticMessage = "Hello from static Outer field!";
static class StaticNested {
void display() {
System.out.println("Outer static message: " + staticMessage); // ✅ Can access static fields
}
}
}
public class Main {
public static void main(String[] args) {
Outer.StaticNested nested = new Outer.StaticNested();
nested.display();
}
}
Output
Outer static message: Hello from static Outer field!
✅ A static nested class can only access static
fields of the outer class.
3. Accessing Outer Class Fields from a Static Nested Class (Using an Instance)
If you want a static nested class to access non-static fields of the outer class, you need to pass an instance of the outer class.
Example: Accessing Non-Static Fields from a Static Nested Class
class Outer {
private String message = "Hello from Outer instance!";
static class StaticNested {
void display(Outer outer) { // Accepts Outer class instance
System.out.println("Outer message: " + outer.message);
}
}
}
public class Main {
public static void main(String[] args) {
Outer outer = new Outer();
Outer.StaticNested nested = new Outer.StaticNested();
nested.display(outer); // Passing outer class instance
}
}
Output
Outer message: Hello from Outer instance!
✅ A static nested class can access instance fields if passed an Outer
instance.
4. Accessing Outer Class Fields Explicitly Using OuterClass.this
If the inner class has a field with the same name as an outer class field, you can use OuterClass.this.fieldName
to refer to the outer class field explicitly.
Example: Using OuterClass.this
to Resolve Field Name Conflict
class Outer {
private String message = "Outer Message";
class Inner {
private String message = "Inner Message";
void display() {
System.out.println("Inner message: " + message); // Accesses inner class field
System.out.println("Outer message: " + Outer.this.message); // Accesses outer class field
}
}
}
public class Main {
public static void main(String[] args) {
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.display();
}
}
Output
Inner message: Inner Message
Outer message: Outer Message
✅ Outer.this.message
is used to refer to the outer class field when there’s a naming conflict.
Final Summary
Nested Class Type | Can Access Outer Class Fields? | How? |
---|---|---|
Non-Static Inner Class | ✅ Yes, including private fields | Directly (has reference to outer instance) |
Static Nested Class | ❌ No (only static fields) | Only accesses static fields of the outer class |
Static Nested Class (With Outer Instance) | ✅ Yes | Needs an instance of the outer class |
Inner Class (Field Name Conflict) | ✅ Yes | Use OuterClass.this.fieldName |