Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add a function to enums in Java?

People also ask

Can you add methods to enum Java?

Enum Class in JavaAn enum class can include methods and fields just like regular classes. When we create an enum class, the compiler will create instances (objects) of each enum constants.

Can enums have user defined methods in Java?

In Java (from 1.5), enums are represented using enum data type. Java enums are more powerful than C/C++ enums. In Java, we can also add variables, methods, and constructors to it. The main objective of enum is to define our own data types(Enumerated Data Types).

Can you assign a value to an enum in Java?

By default enums have their own string values, we can also assign some custom values to enums.

Can enums have fields Java?

Although the enum type has special behavior in Java, we can add constructors, fields and methods as we do with other classes.


Yes Enum is a class in Java:

public enum Animal 
{
  ELEPHANT(true),
  GIRAFFE(true),
  TURTLE(false),
  SNAKE(false),
  FROG(false);

  private final boolean mammal; 
  private Animal(final boolean mammal) { this.mammal = mammal; }
  public boolean isMammal() { return this.mammal; }
}

but in your case for a real system I would make that an Enum as well since there is a fixed set of types of animals.

public enum Type
{
  AMPHIBIAN,
  MAMMAL,
  REPTILE,
  BIRD
}

public enum Animal 
{
  ELEPHANT(Type.MAMMAL),
  GIRAFFE(Type.MAMMAL),
  TURTLE(Type.REPTILE),
  SNAKE(Type.REPTILE),
  FROG(Type.AMPHIBIAN);

  private final Type type; 
  private Animal(final Type type) { this.type = type; }
  public boolean isMammal() { return this.type == Type.MAMMAL; }
  public boolean isAmphibian() { return this.type == Type.AMPHIBIAN; }
  public boolean isReptile() { return this.type == Type.REPTILE; }
  // etc...
}

Also note that it is important to make any instance variable final as well.

You can find more details about it in the Java Language Specification.


Yes, you can. It would look like this:

public enum Animal {
  ELEPHANT(false),
  GIRAFFE(false),
  TURTLE(false),
  SNAKE(false),
  FROG(true);

  private final boolean isAmphibian;

  Animal(boolean isAmphibian) {
    this.isAmphibian = isAmphibian;
  }

  public boolean isAmphibian() {
    return this.isAmphibian;
  }
}

Then you would call it like:

Animal.ELEPHANT.isAmphibian()


I have an other option:

public enum Animal {
    ELEPHANT {
        @Override
        boolean isMammal() {
            return true;
        };
        @Override
        boolean isReptile() {
            return false;
        }
    },
    SNAKE {
        @Override
        boolean isMammal() {
            return false;
        };
        @Override
        boolean isReptile() {
            return true;
        }
    };

    abstract boolean isMammal();
    abstract boolean isReptile();
}

No need of external Interface and I am quite sure (did not test) it works also on Java7.


As well as using the techniques above which add a field to the enumerated type you can also use a pure method based approach and polymorphism. This is more "OOP style" but I would not say it is necessarily better.

Unfortunately, you may (see the comment below) need to define an interface:

public interface AnimalTraits {
    default boolean isAmphibian()   { return false; };
    default boolean isReptile()     { return false; };
    default boolean isMammal()      { return false; };
}

But then you can them implement the interface in each of your enumeration elements:

public enum Animal implements AnimalTraits {

     ELEPHANT   { @Override public boolean isMammal()    { return true; } },
     GIRAFFE    { @Override public boolean isMammal()    { return true; } },
     TURTLE     { @Override public boolean isReptile()   { return true; } },
     SNAKE      { @Override public boolean isReptile()   { return true; } },
     FROG       { @Override public boolean isAmphibian() { return true; } }
}

Note that I use default implementations in the interface to cut down on the amount of typing you need in the enum.

Regarding the necessity of the interface: I tried adding the methods in the interface as abstract methods at the top of the enum and Eclipse seemed to allow it and insisted on implementations in the enum elements, but then failed to compile those properly. So it looks like it ought to be possible without an interface, but perhaps it is not yet implemented in the compiler.