Java Enum enumeration can also be used this way

Source: Internet
Author: User

In most programming languages, enum types are a common and essential type of data, and of course, Java is no exception. However, the enum enum type in Java has many unexpected uses, let's take a look at it.

1. You can add variables and methods to the enum

Let's look at a code example:

public enum State {normal ("just normal", 1), Update ("updated", 2), Deleted ("deleted", 3), fired ("masked", 4);        Member variable private String name;        private int index;            Construction method, Note: The construction method cannot be public because the enum cannot be instantiated with the private state (String name, int index) {this.name = name;        This.index = index;                }//Common method public static String getName (int index) {for (State c:state. VALUES ()) {                if (c.getindex () = = index) {return c.name;        }} return null;        }//Get Set method public String GetName () {return name;        } public void SetName (String name) {this.name = name;        } public int GetIndex () {return index;        } public void Setindex (int index) {this.index = index; }    }

From the code above, we can see that the enumeration values are defined, followed by semicolons, and then other variables and methods can be defined. It is also necessary to note that the construction method in the enum cannot be identified with public, in order to prevent the user from instantiating the enum.

2, can be used to define constants

Let's review how to define constants in Java and see the following code:

public static final int normalstate = 1;private static final int updatestate = 2;

Below we can also use Enum enumeration to replace the above constant definition, the code is as follows:

Public enum State {    Normal, Update, Deleted, fired}

There is no grammatical advantage in using enum to define constants in Java, but enum enum types can provide more operational functionality.

3. Implementing an interface in enum

First look at the following code:

Public interface Icanreadstate {        void read ();        String getState ();}    Public enum state implements Icanreadstate {        normal ("normal", 1), Update ("updated", 2), Deleted ("deleted", 3), fired ("masked", 4); 
   private String name;        private int index;        Private state (String name, int index) {            this.name = name;            This.index = index;        }        Interface Method 1        @Override public        String getState () {            return this.name;        }        Interface Method 2        @Override public        Void Read () {            System.out.println (This.index + ":" + this.name);        }    }

Like using an interface in a generic class, an Enum enumeration can also inherit an interface and implement all the methods in the interface, and the advantage is that it makes it easier to sort, compare, and better encapsulate the values in the enumeration.

Summarize

To put it bluntly, an enum enum type is a final class that cannot be inherited, as an example of the state enumeration above, if you look at the byte code of the enum type, it is a class static constant of state type.

The

Java Enum enumeration can also be used in this way

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.