Usage of Enum in Java

Source: Internet
Author: User

1. About Java Enum:

Anyone who has studied C/C ++ or other languages should have a slight knowledge of the Enum type. Enum is generally used to represent a group of constants of the same type. Such as gender, date, month, and color. The benefit of using constants for these attributes is obvious. Not only can the singleton be guaranteed, but equals can be replaced with "=" during comparison. It is a good habit. JDK1.5 does not have the Enum type before. At that time, it is generally replaced by an interface constant. With JavaEnum, you can more closely represent such constants.

2. How to Use Java Enum

Simple usage: JavaEnum simple usage is generally used to represent a group of common constants and can be used to represent a class of constant values of the same type. For example:

 

Gender:

Public enum SexEnum {

Male, female;

}

Color:

Public enum Color {

RED, BLUE, GREEN, BLACK;

}

 

The values in the enumerated objects must be unique.

You can use the Enum type name to directly reference this constant, such as SexEnum. male, Color. RED.

Complex usage: Java provides some built-in methods for enumeration types, and colleagues can use their own methods to enumerate constants. You can easily traverse enumeration objects. See the following example:

 

1. Code 1 WeekDay. java:

 

 

Public enum WeekDay {

Mon ("Monday"), Tue ("Tuesday"), Wed ("Wednesday"), Thu ("Thursday"), Fri (

"Friday"), Sat ("Saturday"), Sun ("Sunday ");

 

 

Private final String day;

 

Private WeekDay (String day ){

This. day = day;

}

 

 

Public static void printDay (int I ){

Switch (I ){

Case 1: System. out. println (WeekDay. Mon); break;

Case 2: System. out. println (WeekDay. Tue); break;

Case 3: System. out. println (WeekDay. Wed); break;

Case 4: System. out. println (WeekDay. Thu); break;

Case 5: System. out. println (WeekDay. Fri); break;

Case 6: System. out. println (WeekDay. Sat); break;

Case 7: System. out. println (WeekDay. Sun); break;

Default: System. out. println ("wrong number! ");

}

}

 

Public String getDay (){

Return day;

}

}

 

2. Code 2 WeekDayTest. java:

 

 

Public class WeekDayTest {

Public static void main (String args []) {

For (WeekDay day: WeekDay. values ()){

System. out. println (day + "===>" + day. getDay ());

}

WeekDay. printDay (5 );

}

}

 

Output result:

Mon ===> Monday

Tue ===> Tuesday

Wed ===> Wednesday

Thu ===> Thursday

Fri ==> Friday

Sat ==> Saturday

Sun ==> Sunday

Fri

3. Java Enum Principle

Although the syntax structure of the Java Enum type is different from that of the java class, it should be said that the difference is large. However, a class file is generated after compilation by the compiler. This class file is decompiled to show that a class is actually generated, which inherits java. lang. Enum <E>.

After WeekDay is decompiled (javap WeekDay command), the following content is obtained (the assembly code is removed ):

Public final class WeekDay extends java. lang. Enum {

Public static final WeekDay Mon;

Public static final WeekDay Tue;

Public static final WeekDay Wed;

Public static final WeekDay Thu;

Public static final WeekDay Fri;

Public static final WeekDay Sat;

Public static final WeekDay Sun;

Static {};

Public static void printDay (int );

Public java. lang. String getDay ();

Public static WeekDay [] values ();

Public static WeekDay valueOf (java. lang. String );

}


Therefore, the Enum type is actually implemented using Java classes and has no new features, except that the java compiler helps us parse and compile the syntax. You can also implement it by yourself. However, since there is such a convenient thing, it will certainly be used.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.