How to Define Constants in Java

There seems to be a lot of confusion around the topic of constants in Java. Some people make use of integers or Strings to define constants, while others make use of enums.

I’ve also come across constants defined in their own interface – where classes that make use of the constants have to implement the interface. This strategy is often referred to as the interface constant design pattern.

Enum Tricks: Two Ways to Extend Enum Functionality

In my previous article, I explained how and why to use enumsinstead of the switch/case control structure in Java code. Here, I will demonstrate how to extend the functionality of existing enums.

Introduction

Java enumis a kind of a compiler magic. In byte code, any enum is represented as a class that extends the abstract class java.lang.Enum and has several static members. Therefore, enum cannot extend any other class orenum: there is no multiple inheritance.