Java-Inner Class Directly Access Outer Class Members

Inner class directly access Outer class Members

Inner class can directly access Outer class members. But the outer class cannot directly access inner class members.

Program

1

Program Source

class Outer {

    int outerData = 10;
    
    void outerValue()
    {
        System.out.println("outerData = "+outerData);
    }
    
    void callInnerMethod()
    {
        Inner obj = new Inner();
        obj.innerValue();
    }
    
    class Inner {
        
        int innerData = 20;

        void innerValue() {
            outerValue();
            System.out.println("innerData = "+innerData);
        }
    }
}

public class Javaapp {

    public static void main(String[] args) {

        Outer outer = new Outer();
        outer.callInnerMethod();
    }
}

Leave a Comment