Java enum Enumeration

Source: Internet
Author: User

Java enum Enumeration

Java is not enumerated before. To use a feature similar to enumeration, that is, to use constants, you can use the following code:

Definition of constants in Java:

PublicclassSex2 {

PublicstaticfinalintGIRL= 1;

PublicstaticfinalintBOY= 2;

PublicstaticvoidMain (String [] args ){

System.Out. Println (Sex2.GIRL);

}

}

However, this method is not intuitive enough to print Sex2.GIRL,The number is 1. So we have the following improvements:

Improvement 1:

PublicclassSex3 {

PublicstaticfinalStringGIRL= GIRL;

PublicstaticfinalStringBOY= BOY;

PublicstaticvoidMain (String [] args ){

System.Out. Println (Sex3.GIRL);

}

}

Now we can print Sex3.GIRLYou can see that it is GIRL, which has a very clear meaning. However, the problem arises again. If I have a method that accepts GIRL or BOY as the parameter, but I cannot guarantee that someone else uses another string, such as "KID", to call the method ". So we have made further improvements:

Improvement 2:

PublicclassSex4 {

PublicstaticfinalSex4GIRL=NewSex4 (GIRL );

PublicstaticfinalSex4BOY=NewSex4 (GIRL );

PrivateStringsex;

PrivateSex4 (Stringsex ){

This. Sex = sex;

}

PublicString toString (){

ReturnSex;

}

}

In this way, you can use the Sex4 type to ensure that the gender can be a BOY or GIRL. For example, the following function definition:

PublicvoidOutSex (Sex4sex ){

System.Out. Println (sex );

}

But now the problem comes again. We only need to use two constants, but we need to write so many lines of code (when defining Sex4). Is there a simple way? The answer is, of course, to simplify our code writing, the Java enum type was born. The following code is equivalent to the above definition of Sex4:

IMPROVEMENT 3: The enum type was born:

PublicenumSexEnum {

GIRL,

BOY

}

My day! I can't believe it. It's so concise! How can we define the location where the user needs to be used:

PublicvoidOutSex (SexEnumsex ){

System.Out. Println (sex );

}

Question: Since Java 1.5 didn't have enum before, what exactly is the enum with more? What is the difference between him and a common class?

Let me tell you, in Java, enum is actually a class. Defining an enum is to define a class. The Decompilation of SexEnum. class confirms this.

Javap-p SexEnum. class

 

Compiled from SexEnum. java

PublicFinal classSexEnum extendsjava. lang. Enum {

Public static final SexEnum GIRL;

Public static final SexEnum BOY;

Private static final SexEnum [] $ VALUES;

Public static SexEnum [] values ();

Public static SexEnum valueOf (java. lang. String );

Private SexEnum ();

Static {};

}

We can see that SexEnum is a class modified by final, inherited from java. lang. Enum. In addition to our own GIRL and BOY members, the class also contains four members and a static code block. Note that the constructor is private! As you may have guessed, the inheritance relationships here and these members are added by the compiler for us. Through decompilation, we can see the JVM bytecode corresponding to these member functions and static code blocks:

Javap-c SexEnum. class

 

Compiled from SexEnum. java

Public final class SexEnum extendsjava. lang. Enum {

Public static final SexEnum GIRL;

 

Public static final SexEnum BOY;

 

Public static SexEnum [] values ();

Code:

0: getstatic #1 // Field $ VALUES: [LSexEnum;

3: invokevirtual #2 // Method [LSexEnum;. clone :() Ljava/lang/Object;

6: checkcast #3 // class [LSexEnum;

9: areturn

 

Public static SexEnum valueOf (java. lang. String );

Code:

0: ldc #4 // class SexEnum

2: aload_0

3: invokestatic #5 // Methodjava/lang/Enum. valueOf :( Ljava/lang/Class; Ljava/lang/String;) Ljava/lang/Enum;

6: checkcast #4 // class SexEnum

9: areturn

 

Static {};

Code:

0: new #4 // class SexEnum

3: dup

4: ldc #7 // String GIRL

6: iconst_0

7: invokespecial #8 // Method :( Ljava/lang/String; I) V

10: putstatic #9 // Field GIRL: LSexEnum;

13: new #4 // class SexEnum

16: dup

17: ldc #10 // String BOY

19: iconst_1

20: invokespecial #8 // Method :( Ljava/lang/String; I) V

23: putstatic #11 // Field BOY: LSexEnum;

26: iconst_2

27: anewarray #4 // class SexEnum

30: dup

31: iconst_0

32: getstatic #9 // Field GIRL: LSexEnum;

35: aastore

36: dup

37: iconst_1

38: getstatic #11 // Field BOY: LSexEnum;

41: aastore

42: putstatic #1 // Field $ VALUES: [LSexEnum;

45: return

}

Obviously, the code corresponding to the static code block completes the initialization of the GIRL and BOY members. I haven't read the JVM bytecode for a long time, and I have forgotten it. To understand the semantics of the bytecode, please read the Java Virtual Machine manual.

 

Conclusion: by defining enumeration, you can ensure that the parameter must be a member of the enumeration and enhance the readability of the Code. Enum is just a syntactic sugar provided by Java [1] (what is a syntactic sugar ?), So that we can define constants. The enum keyword is actually a class, but the compiler will ensure that this class inherits from Enum and automatically add some members to this class.

 

[1] syntax sugar: the Java syntax is changed at the compiler layer, and the JVM is not changed, that is, the bytecode structure is not changed. Syntax sugar in Java also includes: generics and enhanced.

 

 

 

 

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.