Definition and use of enum types in JAVA __java

Source: Internet
Author: User
Tags modifier

Java Development often uses a limited number of constants, which can be defined within the bean as an enum type, so in the maintenance of more convenient to control, so here is a simple record of the definition and usage of enum, convenient for future direct reference use, lazy people plan, hehe ... OK, just look at the code ...

1, java bean definition containing enum type

public class Xxxbean {//other bean variable,... Province ...///other Bean variable Getter,setter method,...  Province .../** * display status, enum enum type definition/public enum showstatus{/** * 0, do not show/Hide (0, "Do not Show"),/** * 1,
		Show * * (1, "show");  Private Integer Code; Corresponds to the first value in parentheses, that is, 0 or 1 private String description;  Corresponding to the second value in parentheses, that is, do not display or display//if there are other defined values in parentheses, you can define the corresponding variable corresponding to it//the Getter,setter method of each variable, note that this is a public-common modifier.
		GetCode () {return code;
		public void Setcode (Integer code) {this.code = code;
		Public String GetDescription () {return description;
		} public void SetDescription (String description) {this.description = description;
			//enum construct method, note that this is a private, proprietary modifier, private showstatus (Integer code, String description) {this.code = code;
		this.description = description;
			//Through code, that is, 0 or 1 to get the corresponding Enum object public static Showstatus Getbycode (Integer code) {Showstatus showstatus = null; for (int i=0; I<showstatus.values (). lengtH
				i++) {showstatus = Showstatus.values () [i];
				if (Code = = Showstatus.getcode ()) {break;
		} return showstatus;

use of 2,enum types

Gets the entire enum object of the Showstatus type of  Hide
showstatus v1 = xxxbean.hide; 

Gets the hide, which is the name of the Enum object in Hide
string v2 = ();//This is the default method

//Gets the code value of the enum object of 0, which is the Hide: 0
  int v3 = XXXBean.Hide.getCode (); 

Gets the hidden, that is, the description value of the Enum object for hide: Hidden

Java enum enum type is basically defined and used, this is the most commonly used, of course, there are other uses, welcome to the brick ...

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: 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.