开发者

Can singleton class be static?

开发者 https://www.devze.com 2023-01-13 15:30 出处:网络
Can si开发者_JAVA技巧ngleton class be static?No. A singleton class is meant to be instantiated, because the term itself refers to an instance; if you make it a static class, you can\'t create a single

Can si开发者_JAVA技巧ngleton class be static?


No. A singleton class is meant to be instantiated, because the term itself refers to an instance; if you make it a static class, you can't create a singleton object out of it.


(Cat, meet pigeons.)

Yes, but only in practice, not in theory.

A singleton is a class that can only be instantiated once. A static class cannot be instantiated, so it cannot be called a singleton.

However, since we're talking about C#, static classes have constructors, so it is in effect being instantiated, and there can only ever be one instance so that to me looks a lot like a singleton.


No. Singleton referes to single instance of the class. Static class does not have instances.


A singleton is by definition an instance, so no.

But, you could have a static class where the methods access a private static variable. But that is just pushing the singleton a level deeper.


No singleton cannot be static

0

精彩评论

暂无评论...
验证码 换一张
取 消