简体   繁体   English

可以将外部类定义为静态并包含内部静态类吗?

[英]Can outer class be defined as static and enclose inner static class?

It correct to define an outer class as static which have inside also a static class? 将外部类定义为静态内部也是静态类是正确的吗? Only one instance of outer and inner classes is needed. 只需要一个外部和内部类的实例。 Can outer class be abstract and it's enclosed class be static? 外部类可以是抽象的,它的封闭类是静态的吗?

No, a top level class can't be static. 不,顶级课程不能是静态的。 The meaning of "static" in a class declaration is only relevant to nested classes. 类声明中“static”的含义与嵌套类有关。 You can certainly have a static nested class within an abstract class though. 但是,您当然可以在抽象类中拥有静态嵌套类。

From the JLS section 8.1.1 : JLS第8.1.1节

The modifier static pertains only to member classes (§8.5.1), not to top level or local or anonymous classes. 修饰符static仅适用于成员类(第8.5.1节),不适用于顶级或本地或匿名类。

Note that if you want "only one instance" of a class, you should potentially make it a singleton - which is entirely separate, and not something which affects the class declaration itself. 请注意,如果您只想要一个类的“只有一个实例”,那么您应该将它设为一个单例 - 这是完全独立的,而不是影响类声明本身的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 外部和内部类以及静态方法 - Outer and Inner class and static methods 静态内部类可以扩展其外部类吗? - Can a static inner class extend its outer class? 静态内部类如何访问外部类的所有静态数据成员和静态成员函数? - How does static inner class can access all the static data members and static member function of outer class? 从静态内部类访问外部类的静态变量 - Access Static variable of Outer class from Static Inner class 如何使用外部非静态类的对象访问静态内部类方法? - How can I access static inner class method using object of outer non static class? 为什么这个静态内部类不能在其外部类上调用非静态方法? - Why can’t this static inner class call a non-static method on its outer class? 内部静态类继承内部静态类,子级的外部类也继承父级的外部类,为什么我不能进行强制转换 - Inner static class inherits inner static class, and the outer class of the child also inherits the outer class of the parent, why I can't do casting 如何使用内部静态类对象访问外部类变量 - How to access outer class variable using inner static class object 在静态内部类内部访问外部类的方法 - Accessing a method of the Outer Class inside a static Inner Class 为什么内部静态类会扩展外部抽象类? - Why would an inner static class extend the outer abstract class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM