Java: enums and type safety

geniot

geniot

Posted on May 28, 2024

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.

💖 💪 🙅 🚩
geniot
geniot

Posted on May 28, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related