DEV Community

geniot
geniot

Posted on

Java: enums and type safety

Using an enum is much better than using a bunch of constants because it provides type-­safe checking. With numeric or String constants, you can pass an invalid value and not find out until runtime. With enums, it is impossible to create an invalid enum value without introducing a compiler error.

This is only true if it's Java talking to Java. But in modern ecosystems most likely it's a Java microservice that parses incoming JSON with some Jackson library. And then when it comes to persistence you are back to fighting enums again that cannot easily get mapped to PostgreSQL fields.

Top comments (0)