java outer class 1

java outer class

class OuterClass {
  int x = 10;

  class InnerClass {
    int y = 5;
  }
}

public class Main {
  public static void main(String[] args) {
    OuterClass myOuter = new OuterClass();
    //OuterClass.InnerClass myInner = new OuterClass.InnerClass();
    OuterClass.InnerClass myInner = myOuter.new InnerClass();
    System.out.println(myInner.y + myOuter.x);
  }
}

Here is what the above code is Doing:
1. We create an instance of the outer class with OuterClass myOuter = new OuterClass();.
2. We then create an instance of the inner class with OuterClass.InnerClass myInner = myOuter.new InnerClass();.
3. We then access the instance variables of each object with myInner.y and myOuter.x.

Similar Posts