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

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

问题描述

为什么非静态内部类不能有静态方法?

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天全站免登陆