java 静态外部类_为什么外部类在Java中不是静态的?

这个问题已经在这里有了答案:            >            Why are you not able to declare a class as static in Java?                                    14个

在Java中,外部类可以是public,final,default或abstract.

为什么不像静态

公共静态类MyClass {}

解决方法:

外部类已经是隐式静态的.

非静态嵌套类(=内部类)意味着内部类隐式对其父类具有引用.

这就是为什么对于嵌套类,您可以区分静态和非静态.对于外部类而言,这没有意义.

这是了解静态/非静态嵌套类之间的区别的示例.您应该了解为什么在外部类中它没有意义.

public class MyClass {

private String anAttributeOfMyClass;

private /*static*/ class MyInnerClass {

public void foo() {

/*

* Here, I can access the attribute of the parent class

* because I implicitly have a reference to it.

* Try to make the nested class static an see the difference.

*/

anAttributeOfMyClass.trim();

}

}

}

标签:nested-class,java,class,static

来源: https://codeday.me/bug/20191009/1878328.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值