Java enumeration Usage and rationale

Source: Internet
Author: User

Usage:

1  Packagecom.wjq.enums;2 3 //User Status4 //0: Normal status (registered)5 //1: Has been blocked (registered)6  Public enumUserState {7NORMAL (0), SEALED (1);8     9     Private Final intvalue;Ten  One     PrivateUserState (intvalue) { A          This. Value =value; -     } -  the      Public intGetValue () { -         returnvalue; -     } -      +      Public Static voidMain (string[] args) { -         //the output is 1 . + System.out.println (UserState.SEALED.getValue ()); A     } at}

  

Principle:

After the compiler compiles, the enumeration class becomes an abstract class, inheriting java.lang.Enum. Enumeration constants that are defined in enumerations become public static final properties, implemented through inner classes. At the same time, the type is the type of the abstract class, the name is the name of the enumeration constant, and the enumeration constant is initialized by a static code block. Enumerations cannot be copied through cloning and serialization and deserialization, guaranteeing that an enumerated constant is a singleton.

Resources

How Java enumeration types are implemented

Java enumeration Usage and rationale

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.