Skip to content

How can I access OuterClass field from InnerClass?

I’ve been learning Java and OOP for a few weeks now.

Yesterday I learned inner class and outer class. I want to know the way to access field of outerClass from InnerClass when they have exactly same name.

Like below, It is possible to access field of outerClass from innerClass when they have different names.

public class OuterClass { 
    public String OuterName;
    public OuterClass(String OuterName) {
        this.OuterName = OuterName;
    }
    public class InstanceInnerClass {
        public String InnerName;
        public InstanceInnerClass(String InnerName) {
            this.InnerName = InnerName;
        }
        // In this method, accessing field in outer and inner class is possible. 
        // when they have different name.
        public void printAllName() {
            System.out.printf("OuterName: %s, InnerName: %sn", OuterName, InnerName);
        }
    }
}

When they have same name, I don’t know how to access both names.

public class OuterClass { 
    public String name; // field which has same name
    public OuterClass(String name) {
        this.name = name;
    }
    public class InstanceInnerClass {
        public String name; // field which has same name
        public InstanceInnerClass(String name) {
            this.name = name;
        }
        public void printAllName() {
            // What should be written in this code 
            // to print both name fields from OuterClass and InnerClass
            System.out.printf("OuterName: %s, InnerName: %sn", name, name);
        }
    }
}

Answer

Simply by doing OuterClass.this and the name of the field from the OuterClass that you want to access, for instance OuterClass.this.OuterName. In your code would look like the following:

public class OuterClass {
    public String name; // field which has same name
    public OuterClass(String name) {
        this.name = name;
    }
    public class InstanceInnerClass {
        public String name; // field which has same name
        public InstanceInnerClass(String name) {
            this.name = name;
        }
        public void printAllName() {
            // What should be written in this code
            // to print both name fields from OuterClass and InnerClass
            System.out.printf("OuterName: %s, InnerName: %sn", name, OuterClass.this.name);
        }
    }
}

When you have two fields with the same name one in the inner class and the other on the outer class (i.e., shadowing), and you try to access from the inner class that field, the compiler assumes that you want to access the field from the inner class, and ignores the other from the outer class. Otherwise, it would be impossible to choose, which variable to use.

Shadowing

If a declaration of a type (such as a member variable or a parameter name) in a particular scope (such as an inner class or a method definition) has the same name as another declaration in the enclosing scope, then the declaration shadows the declaration of the enclosing scope. You cannot refer to a shadowed declaration by its name alone.