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