What is the purpose of the Enum class that was introduced in Java 5?

My suggestions:

1) either enumexist only before compilation (for example, generics, but I never heard anything about it while he wrote everywhere that generic files are erased after compilation)

2), or Enum is a way to maintain backward compatibility in some way (but I don't see it yet).

Any other suggestions? (by the way, have you ever used it in your code?)

UPD: http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Enum.html

In other words, there is a keyword enumand enum. They both appeared in Java 5. Question: why should we both?

Second question: why enumbecame part of the API?

+3
source share
8 answers

From Java Tutorials :

You should use enumeration types anytime you need to represent a fixed set of constants. This includes natural types of enumerations, such as planets in our solar system and data sets where you know all the possible values ​​at compile time, such as menu selections, command line flags, etc.

Enumerations are extremely useful, and yes, I have used them many times in my code.


Edit:

... there is an enum keyword and an Enum class. They both appeared in Java 5. Question: why do we need both?

This question is similar to the query "there is a keyword classand a class Object. Why do we need both?"

; , ( , ).

q & a.

... - ?

- "". , Enum#valueOf() - , :

DayOfWeek d = Enum.valueOf(DayOfWeek.class, "TUESDAY");

, , :

DayOfWeek d = DayOfWeek.valueOf("TUESDAY");
+9

, ( , int ).

, Enum - Enum s. , Enum ( ).

, ? -, , ( , ). , , Enum<E extends Enum<E>>, , - . , . , , , , .

+4

, .

, , . , "" "". , , :

public void doTransaction(String type)
{
  if (type.equals("sale"))
    ... do whatever ...
}

- - "" "". , , .

, :

public final static int SALE=1, RETURN=2;
public void doTransaction(int type)
{
  if (type==SALE)
  ... do whatever ...
}

, . , :

public final static int SALE=1, RETURN=2;
public final static int DOMESTIC=1, FOREIGN=2;
public void doTransaction(int type, int locale)
{
  if (type==SALE && locale==DOMESTIC)
  ... etc ...
}

- doTransaction(FOREIGN, SALE). , . , , .

Enums .

enum SaleType {SALE, RETURN};
enum Locale {DOMESTIC, FOREIGN};
public void doTransaction(SaleType type, Locale locale)
... etc ...

, - , doTransaction(Locale.FOREIGN, SaleType.SALE), , .

+3

Enum, , , . , ?

.

, Enum .

, Enums , Enum, valueOf ..

, . . , java.lang.Enum

+1

, enum , class. - Java , . Java.

+1
0

# 2. Enums Java. ,

public static final int OPTION_ONE = 1;
public static final int OPTION_TWO = 2;

public enum Option{one, two}

, :

public void doSomething(Option o);

, , public void doSomething(int i), . , , . .

Enum, . , sort.

- , , .

public enum Sort {
        Everyone {
            @Override
            List<Checkin> refreshList(Client client, Location location) throws IOException {
                return client.checkins().getList();
            }
        },
        NearBy {
            @Override
            List<Checkin> refreshList(Client client, Location location) throws IOException {
                return client.checkinsNear(location.getLatitude(), location.getLongitude()).getList();
            }
        }; // and any other sorts

        abstract List<Checkin> refreshList(Client client, Location location) throws IOException;
    }
0
source

Source: https://habr.com/ru/post/1774557/


All Articles