Why You Should Forget About Improving Your waptrick

The Gibson Les Paul guitar was conceived within the extremely starting of electric powered guitar history and has held its location with the forefront of guitar engineering ever since. The two…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Effective Java

Chapter 6: Enums and Annotations

An enumerated type is a type whose legal values consist of a fixed set of constants, e.g. a Year Enum class can have 4 values: Spring, Summer, Fall and Winter . Before Enum type was added to Java, to represent an enumerated type, a set of Int variables will be used like this, known as the int enum pattern:

There are a few disadvantages of using the int enum pattern. First of all, it does not provide any type-safety. Since underlining values are all integers, the int enum pattern cannot contain other useful information about what type the enum actually represents. For example, following int enum pattern, we have:

Another disadvantage of int enum type is when int values are compiled into the client program that uses them. Therefore, if an int value is changed, the client program has to recompile, otherwise the behavior won’t be correct.

Finally, as a group of constant variables that should share some common qualities, by using int enum pattern, there is no reliable way to iterate through the values or know the total size of this group. It will also be hard to print out useful information containing the name of the variable since it will be just an integer.

Compared to int enum pattern, the Enum type in Java is a full class that is effectively final, since there’s no accessible constructor and the Enum class is not extendable. An Enum class can also guarantee compile-time type-safety. If values in an Enum class got reordered or changed, it will not cause an issue in client program since not recompilation is needed. Enum can also provide more useful information with the toString method.

You can also add custom methods and fields to a Enum type to make a rich Enum type. For example, a Enum class of our solar systems can have the following structure:

Here, by providing a constructor, data can be calculated and stored in the fields. In the main method, by iterating through Planet.values(), each planet’s surface weight will be calculated by calling the public surfaceWeight(mass) method, which is done by calculating the multiply of mass(passed in main method), and the surfaceGravity(calculated when initialized each planet).

Many enums are naturally associated with a single int value. The ordinal method in enum classes returns the numerical position of each enum. It is bad practice to use ordinal because 1) it is very hard for maintenance when the enums switch order/modified/deleted; 2) if want specific integer, has to create dummy enums to reach that number. If associating an enum with an int is needed, instance field can be used like this:

Bit field representations are often used when we want to do an OR operation on objects that are represented by bit, so it can mean a set of multiple objects being used at the same time. Bit Field is usually represented with integers. Therefore, in addition to all the disadvantages that int enum has, using bit field needs the user to predict the maximum that will be needed in the future. A better alternative is to use EnumSet, which is a cleaner way to represent a set off enums.

For example, using ordinal indexing in an array could cause errors, because arrays don’t allow generic types, so often an unchecked cast is needed. Also if the ordinal int value is wrong, we cannot guarantee the array will access the correct thing, or access within the array boundary. An alternative is EnumMap by using new EnumMap(ExampleEnum.class). There is no need for an unsafe cast.

Sometimes it is desirable to extend an enum type. For example, here is a extensible Operation class:

While the enum type BasicOperation is not extensible, the interface Operation is. We can add a new operation by implementing the existing interface:

A minor disadvantage of the use of interfaces to emulate extensible enums is that implementations cannot be inherited from one enum type to another.

An example of annotation implementation of @Test :

The declaration for the Test annotation type is itself annotated with Retention and Target annotations. Such annotations on annotation type declarations are known as meta-annotations. The @Retention(RetentionPolicy.RUNTIME) meta-annotation indicates that Test annotations should be retained at runtime. Without it, Test annotations would be invisible to the test tool. The @Target.get(ElementType.METHOD) meta-annotation indicates that the Test annotation is legal only on method declarations: it cannot be applied to class declarations, field declarations, or other program elements.

To override a method, the programmer must provide a method with the same parameter type(s). Adding a @Override annotation can make the compiler check if the types are the same. Override whenever you believe a method should override a super class.

Add a comment

Related posts:

SEO Freelancer

Want To Grow Your Business

What is java and history of java

Java Programming Language is used to run Web Application, Program or Software. Looks like you may not have understood it completely and now you have this question revolving in your mind that Java…

Little Foot

Richard Harrington is a political curator of current events, who covers all issues of importance for conservatives. He brings attention and insight from what happens in the White House to the streets…