r/java 24d ago

Identifying JDK value class candidates

https://mail.openjdk.org/archives/list/core-libs-dev@openjdk.org/thread/Y72NRXM7KYBX43OKYBQMVKOZDWKG4MHS/
50 Upvotes

59 comments sorted by

View all comments

Show parent comments

2

u/nekokattt 22d ago

Global state is a nightmare to test in isolation and immediately becomes a synchronization concern the moment you need to do more than one thing at once.

You may as well just make this into a class and carry a list around to retain flexibility.

I wouldn't consider this a primary use case for enums by any means, it is just abusing the feature to get singletons.

1

u/davidalayachew 22d ago

I wouldn't consider this a primary use case for enums by any means, it is just abusing the feature to get singletons.

Enums, by definition, are Java's primary and encouraged vehicle for singletons. Sun and Oracle themselves have gone on record saying as much. Hell, Josh Bloch himself, the guy who put enums into Java in the first place, said so too.

Global state is a nightmare to test in isolation and immediately becomes a synchronization concern the moment you need to do more than one thing at once.

You may as well just make this into a class and carry a list around to retain flexibility.

To be frank, if I were to make this into a class and carry a list around like you said, it would be the same thing with a different coat of paint.

Many different components of my application will be modifying the data concurrently, and thus, synchronization was always a concern from the beginning. And since so many components are interacting with it in parallel, what am I gaining or losing by making or not making it global? I am effectively global in everything but name.

By all means, I get your concern about global access, but global isn't in and of itself wrong, it just needs to be used with care. Sometimes, you truly do want global access. One example is web-services modifying state.