简体   繁体   中英

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?

There are no consequences. Basically singleton is an instance (just one instance) so it is against design to have class member fields (static ones). I would avoid using static fields

The singleton design pattern is to ensure that there is only one instance of your class in the application run-time. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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