Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java without singletons

Merely out of interrest: do you think Java would have been a better language if static variables would have been excluded, effectively replacing Singletons with singletons? Definition here.

If you think so: could you elaborate on what motivation there might have been for including it in the language?

like image 399
Jonas Byström Avatar asked Dec 21 '25 03:12

Jonas Byström


1 Answers

The same article that you cite has the following statement:

The other kind of Singletons, which are semi-acceptable are those which don't effect the execution of your code, They have no "side effects"

... and then the article explains about logging. Another typical example is Printing. So that are arguments for Singletons even in the article that calls for "let's-get-rid-of-all-singletons".

The argument the author provides is interesting. He states that having global state is the real problem with Singletons, and as long as there is a one-way communication between your program and the Singleton you are in the clear.

And definitively Java would be a worse language if it had no static variables, as it is a feature that is required in several use cases. I mean, you could program in a procedural way in Java if you really wanted... and that is not evil per-se.

The abuse of static variables and Singletons doesn't necessarily mean that we need to get rid of them.

like image 114
Mario Ortegón Avatar answered Dec 22 '25 19:12

Mario Ortegón



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!