Java enum enum

Source: Internet
Author: User

Http://www.cnblogs.com/wenruo/p/5349614.html

The enumeration of Java is implemented by the keyword enum. Can be understood as a class, but this class is automatically added by the compiler to some methods.

The static values () method is used to produce an array of these constant values in the order in which the enum constants are declared

Ordinal indicates the order of declarations for a particular enum constant

ToString () to display the name of each enum instance

Can be used in a switch statement, but note that the case is directly written with constants, without the name of enum.

1 enumspiciness {2     //expressed in uppercase letters3Not, MILD, MEDIUM, hot, flaming,//<--here comma dispensable4 };5 6  Public classEnumdemo {7      Public Static voidMain (string[] args) {8         //Range for9          for(spiciness s:spiciness.values ()) {TenSYSTEM.OUT.PRINTLN (S + ", ordinal" +s.ordinal ()); One         } A          -spiciness[] sp =NewSpiciness[2]; -Sp[0] =spiciness.flaming; theSP[1] =Spiciness.medium; -          -         Switch(sp[0]) { -              CaseNOT:System.out.println ("not spicy @ all");  +                        Break; -              CaseMILD://Note not spiciness. MILD +              CaseMEDIUM:System.out.println ("A Little not");  A                           Break; at              CaseHot : -              CaseFlaming: -             default: System.out.println ("Maybe Too hot"); -         } -     } -}

Java enum enum

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.