scope of private constructor in Nested Class

匆匆过客 提交于 2019-12-03 02:19:46

if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.6.1

Because nested classes can see each others members. This has nothing to do with the static declarations. See the following example of your code with just nested inner classes (not static).

public class PrivateBaseConstructor {
    public class BaseClass {
        private BaseClass() {}
    }

    public class DerivedClass extends BaseClass {
        public DerivedClass() {
            super(); // 1*
        }
    }

    public static void main(String[] args)
    {
       new PrivateBaseConstructor(). new DerivedClass();
    }
}

Read more about nested classes here: http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Because anything declared inside a class can access its private members, including inner classes. However, if you run PMD on your class, you'll find it suggests you change the visibility of the constructor to not-private.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!