Application of Java enumeration type

Source: Internet
Author: User

JDK1.5 introduces a new type--enumeration. Although it is a "small" function in Java, it is very helpful for development.

Related uses of enumerations

Usage One:

Before JDK1.5, we defined constants: public static fianl ..... Well now, with the enumeration, you can put

Related constants are grouped into an enumeration type, and enumerations provide more methods than constants.

 Public enum Color {    RED, GREEN, BLANK, YELLOW  

Usage Two:

The switch statement before JDK1.6 only supports int,char,enum types, and using enumerations can make our code more readable.

enumSignal {GREEN, YELLOW, RED} Public classtrafficlight {Signal color=signal.red;  Public voidChange () {Switch(color) { CaseRed:color=Signal.green;  Break;  CaseYellow:color=signal.red;  Break;  CaseGreen:color=Signal.yellow;  Break; }      }  }  

Usage Three:

If you intend to customize your own method, you must add a semicolon at the end of the enum instance sequence. and Java

Requires that an enum instance be defined first.

 Public enumColor {RED ("Red", 1), Green ("green", 2), BLANK ("White", 3), Yello ("Yellow", 4); //member Variables    PrivateString name; Private intindex; //Construction Method    PrivateColor (String name,intindex) {           This. Name =name;  This. index =index; }      //Common Methods     Public StaticString GetName (intindex) {           for(Color c:color.values ()) {if(C.getindex () = =index) {                  returnC.name; }          }          return NULL; }      //Get Set Method     PublicString GetName () {returnname; }       Public voidsetName (String name) { This. Name =name; }       Public intGetIndex () {returnindex; }       Public voidSetindex (intindex) {           This. index =index; }  }  

For more information on the use of enums:

http://blog.lichengwu.cn/java/2011/09/26/the-usage-of-enum-in-java/

52180865

Specifications that are recommended when using an enum

1. Enumeration class names are suggested with an enum suffix, and enumeration member names need to be all uppercase, and the words are separated by underscores.
Description: The enumeration is a special constant class, and the construction method is forced to private by default.
Example:
Enumeration Name: Dealstatusenum
Member Name: Success/unkown_reason
2. All enum type fields must have a comment indicating the purpose of each data item
3, the enumeration type objects between the value comparison, you can use = =, directly to compare the value is equal, not must use the Equals method

4. The constructor of an enumeration class can only be private.

 

Application of Java enumeration type

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.