Introduction to the use of Enum types in the Java language

Source: Internet
Author: User
Tags definition class definition constant constant definition final static class

An introduction to the type of Enum

The enumeration type (enumerated type) appears early in the programming language and is used to include a similar set of values into one type. The name of this enumeration type is defined as a unique type descriptor, which is similar to the definition of a constant. However, compared to a constant type, an enumeration type can provide a larger range of values for the declared variable.

For example, if you want to paint seven colors for a rainbow, you can do it in a Java program by defining the constants.

Listing 1. Constant definition

public static class Rainbowcolor { 
       
   //red orange-yellow-green cyan Violet seven-color constants define public
   static final int RED = 0; 
   public static final int ORANGE = 1; 
   public static final int yellow = 2; 
   public static final int GREEN = 3; 
   public static final int cyan = 4; 
   public static final int BLUE = 5; 
   public static final int PURPLE = 6; 
}

When used, you can refer to these constants directly in your program. However, there are still some problems with this approach.

Type is not safe

Because the corresponding value of the color constant is an integer, it is very likely that the program executes an arbitrary integer value to the color variable, resulting in an error.

No namespaces

Because the color constants are just properties of the class, you have to access them by class when you use them.

Consistency difference

Because the cosmetic enumeration is a compile-time constant, when the compilation process is complete, all client and server-side references are written directly to the integer value. This way, when you modify an old enumerated integer value or add a new enumeration value, all of the referenced local code needs to be recompiled, otherwise the run-time error occurs.

Type without indication of meaning

Because the color enumeration value is just some integer value without any meaning, if you are debugging at runtime, you will find that there are many magic numbers in the log, but it is difficult for others to understand the secret except the programmer itself.

How to define an Enum type

To improve the Java language deficiencies in this area, the 5.0 SDK was released at the language level with the enumeration type added. The definition of an enumeration type is also very simple, with the enum keyword plus the enumerated body of the name and curly braces, such as the rainbow color mentioned above can be redefined with a new Enum method:

Enum Rainbowcolor {RED, ORANGE, yellow, GREEN, cyan, BLUE, PURPLE}

From the definition above, it seems that the enumeration type in Java is very simple, but in fact the Java language Specification gives the enumeration type a powerful function, not only to simply convert an integer value into an object, but rather to transform the enumeration type definition into a fully functional class definition. This type-defined extension allows developers to add any methods and attributes to the enumeration type, or to implement any interface. In addition, the Java platform provides a high quality implementation for the Enum type, such as the default implementation of comparable and Serializable interfaces, so that developers generally do not care about these details.

Back to the topic of this article, what benefits can the introduction of enum types bring to our development? One of the most immediate benefits is expanding the use of switch statements. Before 5.0, the value of a switch in Java could only be a simple type, such as int, long, char, and then the object could be used after the enumeration type. As a result, the program's control options become more convenient, looking at the following example:

Listing 2. Define Enum Type

Defines an enumeration type of seven days a week public         
enum Weekdayenum {Mon, Tue, Wed, Thu, Fri, Sat, Sun} 
    
//Read the day's message
weekdayenum today = RE Adtoday (); 
    
Select the active
switch (today) { 
 mon:do something by date; 
 Tue:do something; break; 
 Wed:do something; break; 
 Thu:do something; break; 
 Fri:do something; break; 
 Sat:play Sports game; break; 
 Sun:have a rest; break; 

For the dates of these enumerations, the JVM constructs a simple object instance one by one counterpart at run time. Each of these objects has a unique identity, like an integer value, in which a switch statement performs a jump.

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.