为什么我们不能在(非静态)内部类中有静态方法? [英] Why can't we have static method in a (non-static) inner class?

查看:120
本文介绍了为什么我们不能在(非静态)内部类中有静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们不能在非静态内部类中有静态方法?

Why can't we have static method in a non-static inner class ?

如果我让内部类静态它工作。为什么?

If I make the inner class static it works. Why ?

推荐答案

由于内部类的实例与其外部类的实例隐式关联,因此无法定义任何静态方法本身。由于静态嵌套类不能直接引用在其封闭类中定义的实例变量或方法,因此它只能通过对象引用来使用它们,所以可以安全地在静态嵌套类中声明静态方法。

Because an instance of an inner class is implicitly associated with an instance of its outer class, it cannot define any static methods itself. Since a static nested class cannot refer directly to instance variables or methods defined in its enclosing class, it can use them only through an object reference, it's safe to declare static methods in a static nested class.

这篇关于为什么我们不能在(非静态)内部类中有静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆