Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singleton class: static properties or non-static properties?

I´m programming a Class which acts as a Singleton. I was wondering, does it make sense to have non-static properties for this class?

Pseudocode example:

class Foo extends MySingletonClass {

    private static string bar;
    private string baz;

    /* more code here */

}
like image 979
Yeroon Avatar asked Nov 30 '25 15:11

Yeroon


1 Answers

It's not wrong to have static properties, but it's redundant in a singleton.

Also, if you have static properties, and later you need to change the class to not be a singleton no more, you'll need to change the properties either (as every code that access it). So I recommend you not to mark as static, unless really needed.

like image 133
The Student Avatar answered Dec 05 '25 11:12

The Student



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!