[Effective Java Reading notes] 6th Zhang Yi Lifting and annotations

Source: Internet
Author: User

30th use enum instead of int

In all, there are several advantages to using enum

1. Compile-time type safety,

2. Can be guaranteed to be the value of their own definition, there is no monthly risk,

3. Each enum type has its own namespace

4. Enumeration can add arbitrary methods and fields

5. The enumeration type is final and cannot be instantiated, that is, the instance is controllable.

31st Replace ordinal with instance field

That is, do not rely on enumerations to specify their values in order to define them, but should say that the enumeration is stored in an instance field, similar to the value of Xiamen:

 Public enumRewardtype {RMB (1), OBJECT (2), ZAN (3), OTHERS (4), Noreward (5); Private Final intvalue; PrivateRewardtype (intvalue) {         This. Value =value; }     Public intGetValue () {returnvalue; }}

32nd use Enumset instead of bit field

The Enumset class is used to replace the bit field, and the specified enumeration value is written to a enumset type, which is read to know whether a particular enumeration value has been set

33rd replace ordinal index with Enummap

Which is the key to the map using the enumeration value.

35th annotations take precedence over naming patterns

37th Use Override

[Effective Java Reading notes] 6th Zhang Yi Lifting and annotations

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.