How to Access Outer Class From Inner Class in Java


In this example we will show how to access outer class from inner class in Java.

Source Code

package com.beginner.examples;

class Out {
  String a = "test";

  class In {
    public String test() {
      return a;
    }
  }
}

public class Main {
  public static void main(String args[]) {
    Out out = new Out();
    Out.In in = out.new In();
    System.out.println(in.test());// call inner class
  }
}

Output:

test
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments