Enum base class enum details

Source: Internet
Author: User

  This article is mainly about enumeration types of the base class enum class to do an introduction:

First, the Enum class is located under the Java.lang package, and according to the class's introduction, the Enum class is the parent of all enumerated classes in Java, and it is highly efficient to use enumerations as a set or map keys.

The enum class is an abstract class that implements the comparable and serializable interfaces, with only two member variables name and ordinal in the class, each representing the name and ordinal of the enumeration as private. However, two methods are provided to return the two fields, namely name () and ordinal ();

The enum class provides a constructor method that passes two parameters, name and ordinal, and assigns both values to the class member variable name and ordinal, but this constructor is a protected type, which the programmer cannot call, not in detail.

The Enum class has methods that all classes have, ToString and hashcode, and equals, where the ToString () method returns the value of the class member variable name.

The next step is to provide a method called Clone (), which does nothing, but throws an exception clonenotsupportedexception, which is primarily to prevent the enumeration type from being Clone (copy mode in design mode). Doing so guarantees that the enumeration is singleton.

The Getdeclaringclass () method is used to return a class object of the enumeration type where the enumeration constant is located, and it is possible to determine whether two enumerated constants belong to the same enumeration class e1.gedeclaringclass () = = E2.getDeclaring.class ();

ValueOf (): The function of this method is to return a constant named name in the enumeration E, such as

public enum color{

Red,blue

}

public class client{

public void Client () {

Color red = color.valueof ("Red")

}

}

The Enum class also provides a finalize () method that does not implement any code that is intended to ensure that the enumeration does not have a Finalize method, and that the Finalize method is intended to do some extra cleanup when the object is garbage collected. See the Java garbage collection mechanism for details.

The enum class provides ReadObject and Readobjectnodata two methods that only throw exceptions, which are intended to prevent enumeration types from being deserialized.

Why do enumerations want to disallow clone and suppress deserialization?

Because enumerations are a singleton pattern based on the underlying implementation, singleton patterns can often be implemented by cloning and deserialization to produce a singleton class of different objects, where cloning and deserialization are prevented from happening, see the singleton pattern in design mode for details.

Enum base class enum details

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.