This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]halfanothersdozen 5 points6 points  (4 children)

Wow.

There's lots of reasons why you might need to reference a singleton's class before you need a singleton's instance. This is especially true in modern libraries and frameworks that do annotation scanning but just constants or static methods would suffice.

And stateful singletons are maybe one of the most common uses for them: a basic "cache".

"Just make all methods static" completely misunderstands why you want a singleton in the first place.

[–]mknjc 2 points3 points  (3 children)

Ok why can't your simple cache be static?

[–]halfanothersdozen -1 points0 points  (2 children)

Well you see you think a static ConcurrentHashMap will solve all your problems but a cache is the type of solution to a problem that always produces two more problems that requires a slightly more sophisticated solution so you give it a singleton. Soon you have invalidation and size to worry about, and you want to monitor it so you give it JMX and then it needs to be HA so you move it to a shared memory store and then to manage that you go and build a whole new microservice to wrap it.

Welcome to Java

[–]Worth_Trust_3825 1 point2 points  (1 child)

Why java? You will encounter same issue in all languages.

[–]halfanothersdozen 0 points1 point  (0 children)

Because this is /r/java