为什么内部类不能声明静态成员? [英] Why can't inner classes declare static members?

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

问题描述

Java Tutorial说,由于内部类与封闭类的实例相关联,因此它(内部类)无法定义任何静态成员本身。

The Java Tutorial says that since an inner class is associated with an instance of the enclosing class, it (the inner class) cannot define any static members itself.

对我来说有趣的是为什么内部类不能声明静态成员,例如,一些私有静态字段,这个内部类的实例可能与其他实例共享同一个内部阶级?这只是Java中的一个实现,必须被视为理所当然或其他什么?

It's interesting for me why can't inner classes declare static members for instance, some private static field, which the instance of this inner class could possibly share with the other instances of the same inner class? is this just an implementation of things in Java that has to be taken for granted or something else?

推荐答案

基本上只是一个武断的决定。没有理由无法支持,但 支持它也没有任何正当理由。只是声明外部类中的静态字段。

Basically just an arbitrary decision. there's no reason it couldn't be supported, but there is also not really any good reason to support it. just declare the static field in the outer class.

同样,该引用可能不完全正确:我相信你可以在内部类中声明一个静态serialVersionUID。

also, that quote may not be entirely correct: i believe you can declare a static serialVersionUID in an inner class.

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

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