A brief introduction to <java> enumeration

Source: Internet
Author: User

1. Enum, enum keyword, equivalent to public final static.

2. For example:

An enumeration type named spiciness is defined first.

 Public enum spiciness {Not    , MILD, MEDIUM, Hot, flaming}

To test the enum again, this test method shows that it has the ToString () method, which conveniently displays the name of an enum instance.

 Public class Simpleenumuse {    publicstaticvoid  main (string[] args) {        =  Spiciness.medium;        System.out.println (Howhot);    }}

Output Result: MEDIUM

In addition, Enum also has the ordinal () method, which makes it easy to represent the order of declarations for a particular enum constant.

 Public class Enumorder {    publicstaticvoid  main (string[] args) {        for  (spiciness s:spiciness.values ()) {            + ", ordinal" + s.ordinal ());     }}}

Output Result:

Not, ordinal 0
MILD, ordinal 1
MEDIUM, ordinal 2
Hot, ordinal 3
Flaming, ordinal 4

A brief introduction to <java> enumeration

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