Delve into the Java enum (enum) type _java

Source: Internet
Author: User

Objective

In daily programming, there are often such "datasets," whose values are stable in the program, and the elements in the dataset are finite. For example, from Monday to Sunday, seven data elements constitute a week of "data sets", Four seasons of four data elements constitute the "dataset". How do you better use these "datasets" in Java? So the enumeration came in handy.

An enumeration is actually a type, like int, char, which restricts input when defining variables, and you can only assign values that are specified in an enum.

Enum (enum) implementation

The implementation of the Java enumeration type in JDK5 is not so much a new type as a syntactic sugar.

public enum Season {
 SPRING,
 SUMMER,
 Autumn,
 WINTER
}

Using the Decompile tool to see how this code is implemented, the Decompile code is as follows:

Public final class Season extends Enum {public

 static season[] values () {return
  (season[)) $VALUES. Clone ();
 Public

 static Season valueof (String s) {return
  (Season) enum.valueof (Season, s);
 }

 Private Season (String s, int i) {
  super (S, i);
 }

 public static final Season SPRING;
 public static final Season SUMMER;
 public static final Season autumn;
 public static final Season WINTER;
 private static final Season $VALUES [];

 static {
  spring = new Season ("Spring", 0);
  SUMMER = new Season ("SUMMER", 1);
  Autumn = new Season ("Autumn", 2);
  WINTER = new Season ("WINTER", 3);
  $VALUES = (new season[] {
   SPRING, SUMMER, Autumn, WINTER
  });
 }

The code that you decompile can be found:

1, Season is an ordinary class, inherits from Enum , and through the final keyword modification, avoids being inherited,

2,,, SPRING SUMMER AUTUMN and WINTER is a Season static instance of the class and is initialized in the class constructor <clinit> method.

3 values() . The method returns a copy of the private variable $VALUES[] and $VALUES[] is also initialized in the <clinit> method.

How to use Enumerations (enum)

1. Single Case mode

We already know that a class constructor <clinit> can only be executed by one thread during the initialization phase of class loading, so each instance of the enumeration has and has only one copy in the Java heap, which makes it easy for the enumeration to implement the singleton pattern, which is exactly what effective Java author Josh Bloch Promote the use of ways to implement simple patterns.

public enum Singleton {INSTANCE}

2. Use in switch

3, custom fields and methods

The enumeration (enum) can be customized for business logic in addition to the default fields and methods.

public enum Enumtest {
 plus (' + ') {
  @Override public
  int bind (int arg1, int arg2) {return
   arg1 + arg2;
   
    }
 ,
 SUB ("-") {
  @Override public
  int bind (int arg1, int arg2) {return
   arg1-arg2;
  }}
 ;

 final String operation;

 Enumtest (String operation) {
  this.operation = operation;
 }

 abstract int bind (int arg1, int arg2);
}
   

4. Implement Interface

Interface operation{
 int operate (int arg1, int arg2);
}

Public enum Enumtest implements Operation {

 plus (' + ') {
  @Override public
  int operate (int arg1, int arg2) { C27/>return arg1 + arg2
  }}
 ,
 SUB ("-") {
  @Override public
  int operate (int arg1, int arg2) {
   return arg1-arg2;
  }
 ;

 Final String operation;

 Enumtest (String operation) {
  this.operation = operation;
 }
}

In practical application, "+", "-" can be used as key,plus and sub as value , in advance save in hashMap , the specific use of the following methods:

Operation Operation = hashmap.get ("+");
int result = Operation.bind (1, 2);

Summarize

The above is all about the enumeration types in Java, hope that through this article on the Java enumeration of the introduction, can bring help to everyone. If you have any questions, you can exchange messages.

Related Article

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.