Initial knowledge of Java Enum

Source: Internet
Author: User

An enum, called enumeration, is a new feature introduced in JDK 1.5 and is housed in a Java.lang package.

An enum is a keyword that feels like it is tied to class, interface, and cannot be used with final,abstract because it defaults to the final type.

Each enum element in an enum class is a static final instance of its class

An enum is an enumeration type that is defined as "STAITC final", which means that it cannot be overridden and inherited, is assigned during initialization, and cannot be changed

Enum-decorated classes cannot be inherited by other classes, nor can they inherit other classes. The reason is that enum inherits the Enum class by default, so it cannot be multiple, and the class that is decorated by enum defaults to final, so it cannot derive subclasses.

The simplest example
1 enumEnumdata {2 MON, TUE, WED, THU, FRI, SAT, SUN;3 }4  Public classEnumtest {5      Public Static voidMain (string[] args) {6 System.out.println (Enumdata.mon);7          for(Enumdata e:enumdata.values ()) {8 System.out.println (e.tostring ());9         }Ten     } One}

Syntax (definition)

Creating an enumeration type to use the enum keyword implies that the type created is a subclass of the Java.lang.Enum class (Java.lang.Enum is an abstract class). The enumeration type conforms to the generic pattern Class enum<e extends Enum<e>>, and E represents the name of the enumeration type. Each value of the enumeration type is mapped to the protected enum (string name, int ordinal) constructor, where each value's name is converted to a string, and the ordinal setting indicates the order in which this setting was created.

public enum Enumtest {
MON, TUE, WED, THU, FRI, SAT, SUN;
}

This code actually calls the Enum 7 times (String name, int ordinal):

New Enum<enumtest> ("MON", 0);
New Enum<enumtest> ("TUE", 1);
New Enum<enumtest> ("WED", 2);
... ...

Introduction to common methods of enum objects

int compareTo(E o)
compares this enumeration with the order of the specified objects.
class<e> getdeclaringclass()
returns the Class object that corresponds to the enumeration type of this enumeration constant.
String name()
returns the name of this enumeration constant, declared in its enumeration declaration.
int ordinal()
returns the ordinal of an enumeration constant (its position in the enumeration declaration, where the initial constant ordinal is 0).
String toString()
returns the name of the enumeration constant, which is contained in the Declaration.
static <t extends enum<t>> T valueOf(class<t> enumtype, String name)
returns an enumeration constant of the specified enumeration type with the specified name.

Reference Links:
[1]http://www.cnblogs.com/hyl8218/p/5088287.html
[2]http://blog.csdn.net/u013248535/article/details/52918940

Use it in detail, or refer to the link!

Initial knowledge of Java 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.