Enum class enum case

Source: Internet
Author: User

Create a new enumeration class:
 Public enum enum1 {red  ("Scarlet"), BLACK, GREEN, YELLOW, BLUE;   Private String cum;   Private enum1 () {//Private construction method (must be set to private because the enumeration itself is a singleton mode.) )  }  private  enum1 (String cum) {    this. Cum= cum;  }    public String getcum () {    return  cum;  }}

The values () method gets all the enumeration constants, and you can use the Foreach Traversal for example:

Determine which enumeration constants belong to the input values
String input = in.nextline (); for (Enum1 name:enum1.values ()) { if(Input.equals (Name.getcum ())) { System.out.println ( name); } Else { System.out.println ("null"); } }

The above method, if the input is red, gets the result:

Input: Red NULL NULL NULL NULL

Enum1.valueof (String str), which is enumerated constants such as:

The result of enum1.valueof ("Red") is: Red

Enum class enum case

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.