Java inner class visibility puzzle

前端 未结 4 1833
天涯浪人
天涯浪人 2021-01-02 07:03

Consider the following case:

public class A {
  public A() { b = new B(); }
  B b;
  private class B { }
}

From a warning in Eclipse I quot

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-02 07:10

    The access of class B and its constructor do not have to be the same. You can have a private inner class with a package-scope constructor, and this is what I usually do.

    public class A {
      public A() { b = new B(); }
      B b;
      private class B {
        B() { }
      }
    }
    

提交回复
热议问题