我正在重构一个正在变大的
android项目.运行lint为我提供了JSME问题外部和内部类之间的私有成员访问.考虑以下示例
public class Outer { private Inner mInner = new Inner(); private class Inner {} }
我得到的信息
Name privatefieldInnermInner Location classOuter (default package) Problem synopsis Access to private member of class 'Inner' at line 2 Problem resolution Make 'Inner' constructor package-local
应用问题解决方案会将源更改为
public class Outer { private Inner mInner = new Inner(); private class Inner { Inner() {} } }
我此刻有点困惑.到现在为止,我认为这个例子相当于
public class Outer { private Inner mInner = new Inner(); private class Inner { public Inner() {} } }
在这种情况下我错了还是皮棉问题?
解决方法
Section 8.8.9 of the Java language specification,“Default constructor”说:
In a class type,if the class is declared public,then the default constructor is implicitly given the access modifier public (§6.6); if the class is declared protected,then the default constructor is implicitly given the access modifier protected (§6.6); if the class is declared private,then the default constructor is implicitly given the access modifier private (§6.6); otherwise,the default constructor has the default access implied by no access modifier.