Application of Java Enumeration

Source: Internet
Author: User

Recent projects, see the predecessors used by the enumeration more, because of their own before the enumeration of this type is not very understanding, so spent scheming to look under, collation record.

1. Enumeration constants

The state fields defined in the system are used more:

 Public enum ordertype{      pending,confirm,cancel,expire,success;}

2. Define a custom method: Personal understanding can be used for conversion fields

 Public Static enumgrouptypeenum{GX ("Funny", 0), FW ("Crazy Play", 1), XX ("Astrology", 2), JH ("Doubts", 3), SS ("Fashion", 4), lt ("chat", 5), ZS ("Real Thing", 6); Private FinalString name; Private Final intindex; PrivateGrouptypeenum (String name,intindex) {             This. Name =name;  This. index =index; }                 Public StaticString GetName (intindex) {             for(Grouptypeenum gEnum:groupTypeEnum.values ()) {if(Genum.index = =index) {                    returnGenum.name; }            }            return""; }    }

Or

 Public enum orderstatus{     Pending ("Pending"),     Confirm ("determined"),     cancel ("canceled"      ), Expire ("Expired"),      Success ("completed");      Private Final String Statusname;     Private orderstatus (String statusname) {           this. statusname = statusname;      }    }  // Value: OrderStatus.Cancel.statusName == Cancel

PS: The corresponding field type setting in database MySQL: If the field is a string, and the length is fixed, you can try char; if it is numeric, you can use tinyint. Of course, MySQL supports enum types, but it can be cumbersome if the data is migrated.

Application of Java Enumeration

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.