How to Access Private Inner Class in Java


In this example, we will learn the method to access private inner class in Java.

Source Code

package com.beginner.examples;

class Out {
  int a = 10;

  private class In { // private inner class
    int b = 5;
  }
}

public class AccessInnerClass {
  public static void main(String[] args) {
    Out out = new Out();
    Out.In in = out.new In();
    System.out.println(in.b + out.a);
  }
}

Output:

error: Out.In has private access in Out
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments