5

I know the usage of enum in java.

Is recommended to use enums for storing the program constants(rather than the class described below)?

public class Constants{
    public static final String DB_CF_NAME = "agent";
    public static final String DB_CF_ID = "agent_id";
    public static final String DB_CF_TEXT = "agent_text";
    public static final String DB_CF_LATITUDE = "latitude";
}
Hosein Masbough
  • 431
  • 1
  • 4
  • 19
  • 3
    Yes. `Enums` (unlike `class`) are meant to be used that way. – TheLostMind Aug 25 '14 at 11:38
  • 1
    Have a read: http://stackoverflow.com/questions/9969690/whats-the-advantage-of-a-java-enum-versus-a-class-with-public-static-final-fiel – Lucas Aug 25 '14 at 11:39
  • 2
    I would actually say that this depends on your constants. Imagine a project where you have constants of different types, `String`, `double`, `int` etc, then an enum would not really fit but rather a class with a lot of `static final` variables. – Pphoenix Aug 25 '14 at 11:48
  • 2
    IMHO, the only true advantages of enum over class for defining constants is that enums are automatically static singletons and are automatically final. You or your users cannot make the mistake of instantiating or overriding your constant container. – hiergiltdiestfu Aug 25 '14 at 11:59
  • Don't forget, ENUM are enumerated data types. Those are data types having a value set. Technically it fits into constants but conceptually they are not...better you use `private` constants in your context. – Not a bug Aug 25 '14 at 12:09

4 Answers4

4

It is recommended to use Enum. The possible reasons are like

  • provides default functions to iterate through constants
  • Can write static functions to get the values based on key

It is possible to make the methods by yourself, to make your class self sustained. but the preferable approach is using Enum

Vinay Veluri
  • 6,671
  • 5
  • 32
  • 56
  • My constants only using for getting rows of database(queries).I dont need to usage of Enums like Iterating and static function.Is it still reasonable that I use this way? – Hosein Masbough Aug 25 '14 at 12:04
  • 1
    Yes, If you use JPA for model classes, use `@Enumerated(EnumType.STRING)` to control the constants around the column. – Vinay Veluri Aug 25 '14 at 12:08
3

Yes, you're right - it is recommended to use enums for storing the program constants.

As you can see on examples below, this approach allows you to:

  • add/override useful methods for your string literals:

    public enum Days {
        MONDAY("Monday"),
        TUESDAY("Tuesday"),
        SUNDAY ("Sunday");
    
    private final String name;
    
    private Day(String s) {
            name = s;
    }
    
  • use built-in methods for Enums:

    public boolean equalsName(String otherName){
        return (otherName == null)? false : name.equals(otherName);
    }
    
  • use EnumMap and EnumSet:

    private EnumSet<Option> badDays = EnumSet.of(Days.MONDAY, Days.TUESDAY);
    

Here is a good discussion about this.

Community
  • 1
  • 1
Kao
  • 7,225
  • 9
  • 41
  • 65
1

This works from Java 1.5 and above:

public enum Constants {
    DB_CF_NAME("agent"),
    DB_CF_ID("agent_id"),
    DB_CF_TEXT("agent_text"),
    DB_CF_LATITUDE("latitude");

    private String name;

    private Constants(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }
}

The only time you need the construct using public static final variables mentioned in the question is when you are programming for Java 1.4 or below. I somewhat hope you'll never need to do that though.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
1

Yes. Exactly. You can use Enum are designed to use as predefined constants.

Enum docs are mentioned the same as well.

An enum type is a special data type that enables for a variable to be a set of predefined constants.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307