Java Enum class

Source: Internet
Author: User

In some cases, the object of a class is limited and fixed, such as the seasonal class, which has only 4 objects. This example of a finite and fixed class, in Java, is called an enumeration class.

In the early days, the enumeration classes might be represented directly using simple static constants, for example:

public static final int season_spring = 1;public static final int season_summer = 2;public static final int season_fail = 3;public static final int season_winter = 4;

This definition is straightforward, but there are some problems:

    1. Type is unsafe: because a season is used as an int integer, for example an addition operation.

    2. There is no namespace: such as the season prefix, otherwise it will be confused with others.

    3. The meaning of the printout is ambiguous: for example, output season_spring, the output is actually 1.

But enumerations do have meaning, so they can be implemented in the early stages by defining classes.

    1. Hides the constructor through private.

    2. All possible instances of this class are saved using the public static final modified class variable.

    3. If necessary, you can provide some static methods that allow other programs to get an instance of the match based on the parameters.

    4. Using an enumeration class can make your program more robust and avoid the randomness of creating objects.

However, it is cumbersome to implement enumerations by defining classes to implement them, and Java has added support for enumeration classes since JDK1.5.

Entry:

Java5 adds an enum keyword (which is the same as the class, interface keyword) to define an enumeration class, which is a special class that can have its own member variables, methods, implement one or more interfaces, or define its own constructor. A Java source file can define up to one enumeration class with a public access permission and the same name.

However, the enumeration class is not a normal class after all, it differs from the normal class in the following:

    1. An enumeration class can implement one or more interfaces, and an enumeration class defined with an enum inherits the Java.lang.Enum class by default, rather than inheriting the object class by default, so the enumeration class cannot display inheritance from other parent classes. Where the Java.lang.Enum class implements the java.lang.Serializable and java.lang.Comparable interfaces.

    2. Enum classes that use enum definitions, non-abstractions, by default use the final adornment, so the enumeration class cannot derive subclasses.

    3. The constructor for an enumeration class can only use the private access control, and if the constructor's access control is omitted, the private adornment is used by default, and only the private adornment is specified if the access control is enforced.

    4. All instances of an enumeration class must appear listed in the first row of the enumeration class, or the enumeration class will never produce an instance. Listing these instances, the system automatically adds a public static final decoration.

The enumeration class provides a value () method by default, which is a convenient way to iterate through all of the enumeration values.

An instance of an enumeration class can only be an enumeration value, not an enumeration class object created arbitrarily by new

This article is from the "where No Play" blog, please be sure to keep this source http://liaofan.blog.51cto.com/12295212/1917025

Java Enum class

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.