加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 私有内部类的构造函数也是私有的吗?

发布时间:2020-12-15 03:04:30 所属栏目:Java 来源:网络整理
导读:我正在重构一个正在变大的 android项目.运行lint为我提供了JSME问题外部和内部类之间的私有成员访问.考虑以下示例 public class Outer { private Inner mInner = new Inner(); private class Inner {}} 我得到的信息 Name privatefieldInnermInnerLocation c
我正在重构一个正在变大的 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读