[Code optimization] Use Enum to replace int Constants

Source: Internet
Author: User

This code is often seen in the Code:

Public static final int app_start = 1;

Public static final int app_pause = 0;

Public static final int app_stop = 2;


Public static final int play_start = 1;

Public static final int play_pause = 0;

Public static final int play_stop = 2;


This method is called the int enumeration mode, which has many shortcomings, but is often written like this. It does not help in type security and ease of use

If you pass the app _ to the play _ method, the compiler will not receive a warning, and the = operation will be used to compare the app _ and play _, or even worse:

Int I = (app_start-play_start)/play_start;


Using int enumeration is a compilation constant. If the value related to the int constant changes, the client can only achieve the effect after re-compilation.


How can we avoid this defect? The Enum enumeration solution was provided in Java:


Public Enum app {start, stop, pause };

Public Enum play {start, stop, pause };


On the surface, there are no differences, but this is not the case. The Java Enumeration type is fully functional, and Java enumeration is essentially an int value.


Enumeration provides type security during compilation.If an app is declared as the parameter type, it is guaranteed that any non-empty object uploaded to this parameter must

It is one of the three valid app attributes and does not reference the play attributes.


Multiple enumerations of constants with the same name can coexist in one system.. Because each type has its own namespace

For example, there is no conflict between app. Start and play. Start.


You can also add any methods and fields to the enumeration type.You can use an appropriate method to enhance the enumeration type, or use it as a set of enumeration constants.

And becomes full-featured abstraction over time


In short, compared with int constants, the advantages of enumeration types are self-evident. enumeration types are much easier to read, safer, and more powerful.

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.