简体   繁体   English

私人 static singleton class 中的最终字段

[英]private static final field in singleton class

Are there any consequences declaring/not declaring a field in a singleton class static final or just final if it's private and only a accessible from the class itself?在 singleton class static final 或 final 中声明/不声明字段是否有任何后果,如果它是私有的并且只能从 class 本身访问?

There are no consequences.没有任何后果。 Basically singleton is an instance (just one instance) so it is against design to have class member fields (static ones).基本上 singleton 是一个实例(只是一个实例),因此反对设计具有 class 成员字段(静态字段)。 I would avoid using static fields我会避免使用 static 字段

The singleton design pattern is to ensure that there is only one instance of your class in the application run-time. singleton设计模式是为了保证你的class在应用运行时只有一个实例。 This Singleton object it will be used by other object in order to invoke it public methods, so you should expose a public static method to other objects in order to get an instance of the singleton, so the field should be static also.这个 Singleton object 它会被其他 object 使用,以便调用它的公共方法,所以你应该公开一个公共的 static 方法给其他对象,以获得 singleton 的实例,所以该字段应该是 8821607087683

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM