Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's difference between enums and final variables? [duplicate]

Tags:

java

enums

I am trying to read up on enums to better understand them.

From the javadocs of enums I get the following:

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

To me this sounds a lot like a final variable.

  1. What is the real difference?
  2. And when would you use one over the other?
  3. Is enum just a final variable that can be a set of things?
like image 940
Marc Rasmussen Avatar asked Dec 18 '13 15:12

Marc Rasmussen


People also ask

What is the difference between enum and final?

The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).

Can enum have duplicates?

CA1069: Enums should not have duplicate values.

Are enum variables final?

An enum type is implicitly final unless it contains at least one enum constant that has a class body. It is a compile-time error to explicitly declare an enum type to be final.

Is it better to use enum or constant?

Enums limit you to the required set of inputs whereas even if you use constant strings you still can use other String not part of your logic. This helps you to not make a mistake, to enter something out of the domain, while entering data and also improves the program readability.


3 Answers

The difference is that enums provide type safety.

Let's have this enum.

public enum MuEnum {
    FIRST("First"), SECOND("Second");

    private String value;

    MyEnum(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }
}

Consider the following example:

public void myMethod(MyEnum parameter) { .. }

Here, you can only pass MyEnum values (FIRST or SECOND), while if your method signature was:

public void myMethod(String parameter) { .. }

you would be able to pass an invalid parameter (some String which content is different than "First" or "Second").

like image 133
Konstantin Yovkov Avatar answered Oct 05 '22 12:10

Konstantin Yovkov


When you have an enum like

public enum MyEnum {
   FIRST_COSTANT, SECOND_CONSTANT;
}

What you actually have is

public class /* enum */ MyEnum extends Enum<MyEnum> {
    private Enum() {}
    public final static MyEnum FIRST_CONSTANT = new MyEnum() {};
    public final static MyEnum SECOND_CONSTANT = new MyEnum() {};    
    ... // methods     
}

So in that sense, yes, they are the same.

This is explained in the Java Language Specification here.

In addition to the members that an enum type E inherits from Enum, for each declared enum constant with the name n, the enum type has an implicitly declared public static final field named n of type E. These fields are considered to be declared in the same order as the corresponding enum constants, before any static fields explicitly declared in the enum type. Each such field is initialized to the enum constant that corresponds to it. Each such field is also considered to be annotated by the same annotations as the corresponding enum constant. The enum constant is said to be created when the corresponding field is initialized.

like image 45
Sotirios Delimanolis Avatar answered Oct 05 '22 11:10

Sotirios Delimanolis


There is a big difference: first, enum, is not a variable, it is a type. A final variable, say, an int, can have a single, preset value from among many possible values of int, while an enum variable (which could also be final) can have a value from a set that you declare when defining the enum.

Here is an example:

enum Color {Red, Green, Blue;}

The declaration above does not define a variable. It defines a set of values a variable of type Color could have, if you choose to declare one:

Color backgroundColor = Color.Red;

Now you have a non-final variable of an enumerated type Color. Its value can change, but it must be one of the three values defined as part of the enum.

In practice, you use an enum when you model a something with a fixed number of states, to which you would like to give a name. You could use one or more final variables for that, too, - in fact, this has been done a lot before enums were introduced. For example, Java's Calendar class uses static final int constants for the various parts of the date, where an enum would have worked better.

like image 33
Sergey Kalinichenko Avatar answered Oct 05 '22 12:10

Sergey Kalinichenko