Enumeration classes and annotations of Java Foundation

Posted by Danno13 on Mon, 27 Sep 2021 02:32:07 +0200

1. Overview of enumeration class

Enumeration classes are strongly recommended when you need to define a set of constants

You need to customize the enumeration class before JDK1.5. The enum keyword added in JDK 1.5 is used to define enumeration classes

Enumeration class properties

  • The properties of enumeration class objects should not be allowed to be changed, so private final should be used
  • The private final decorated property of an enumeration class should be assigned a value in the constructor
  • If the enumeration class explicitly defines a constructor with parameters, the corresponding incoming parameters must also be used when listing the enumeration values

2. User defined enumeration class

Before JDK1.5: Custom enumeration class

  1. Privatize the constructor of the class to ensure that its objects cannot be created outside the class

  2. Create an instance of the enumeration class inside the class. Declared as: public static final

  3. If the object has an instance variable, it should be declared as private final and initialized in the constructor

class Season{
    // 1. The instance variable should be declared as private final and initialized in the constructor
    private final String name;
    private final String desc;

    // 2. Privatize the constructor of the class to ensure that its object cannot be created outside the class
    private Season(String name,String desc){
        this.name = name;
        this.desc = desc;
    }

    // 3. Create an instance of the enumeration class inside the class. Declared as: public static final
    public static final Season SPRING = new Season("spring", "in the warm spring , flowers are coming out with a rush");
    public static final Season SUMMER = new Season("summer", "Summer heat");
    public static final Season AUTUMN = new Season("autumn", "fresh autumn weather");
    public static final Season WINTER = new Season("winter", "snow gleams white");

    // Provide get method
    public String getName() {
        return name;
    }

    public String getDesc() {
        return desc;
    }

    @Override
    public String toString() {
        return "Season{" +
                "name='" + name + '\'' +
                ", desc='" + desc + '\'' +
                '}';
    }
}

3. Use enum to define enumeration classes

  • Enum classes defined with enum inherit the java.lang.Enum class by default, so they cannot inherit other classes
  • The constructor of an enumeration class can only use the private permission modifier
  • All instances of an enumeration class must be explicitly listed in the enumeration class (, delimited; end). The public static final modifier will be automatically added to the listed instances
  • The enumeration class object must be declared on the first line of the enumeration class
public class EnumDemo {
    public static void main(String[] args) {
        System.out.println(Season.AUTUMN);
    }
}
enum Season{
    // Declare the enumeration class object on the first line of the enumeration class
    Season("spring", "in the warm spring , flowers are coming out with a rush"),
    SUMMER("summer", "Summer heat"),
    AUTUMN("autumn", "fresh autumn weather"),
    WINTER("winter", "snow gleams white");

    // 1. The instance variable should be declared as private final and initialized in the constructor
    private final String name;
    private final String desc;

    // 2. Privatize the constructor of the class to ensure that its object cannot be created outside the class
    private Season(String name,String desc){
        this.name = name;
        this.desc = desc;
    }

    // Provide get method
    public String getName() {
        return name;
    }

    public String getDesc() {
        return desc;
    }
}

4. General notes

  • Since JDK 5.0, Java has added support for metadata, that is, annotation
  • Annotation is actually a special tag in the code. These tags can be read and processed during compilation, class loading and runtime. By using annotation, programmers can embed some supplementary information in the source file without changing the original logic. Code analysis tools, development tools and deployment tools can be verified or deployed through these supplementary information
  • Annotation can be used like modifiers to modify the declarations of packages, classes, constructors, methods, member variables, parameters and local variables. This information is stored in the "name=value" pair of annotation

Frame = annotation + reflection + design pattern.

5. User defined annotation

  • Define a new Annotation type using the @ interface keyword
  • Custom annotations automatically inherit the java.lang.annotation.Annotation interface
  • The member variables of Annotation are declared as parameterless methods in the Annotation definition. Its method name and return value define the name and type of the member. We call it configuration parameters. The type can only be an array of eight basic data types, String type, Class type, enum type, Annotation type and all the above types.
  • You can specify the initial value for the Annotation's member variable when it is defined. You can use the default keyword to specify the initial value of the member variable
  • If there is only one parameter member, the parameter name value is recommended
  • If the defined annotation contains configuration parameters, the parameter value must be specified when using, unless it has a default value. The format is "parameter name = parameter value". If there is only one parameter member and the name is value, "value =" can be omitted
  • Annotations without member definitions are called tags; Annotations that contain member variables are called metadata annotations

Note: the user-defined annotation must be accompanied by the annotation information processing process to be meaningful.

JDK5.0 provides four standard meta annotation types: Retention, Target, Documented and Inherited

@Retention: can only be used to decorate an Annotation definition to specify the life cycle of the Annotation, @ retention contains a member variable of RetentionPolicy type. When using @ retention, you must specify a value for the value member variable:

  • RetentionPolicy.SOURCE: valid in the source file (i.e. source file retention). The compiler directly discards the comments of this policy
  • RetentionPolicy.CLASS: valid in the class file (i.e. class retention). When running Java programs, the JVM will not retain annotations. This is the default
  • RetentionPolicy.RUNTIME: valid at run time (i.e. reserved at run time). When running Java programs, the JVM will retain comments. The program can get the comment through reflection.

@Target: used to modify the Annotation definition. It is used to specify which program elements can be modified by the modified Annotation@ Target also contains a member variable named value.

@Documented: used to specify that the Annotation class modified by the meta Annotation will be extracted into a document by the javadoc tool. By default, javadoc does not include annotations. Annotations defined as documented must have the Retention value set to RUNTIME.

@Inherited: the Annotation modified by it will be inherited. If a class uses an Annotation decorated with @ inherited, its subclasses will automatically have the Annotation. For example, if the custom Annotation marked with @ inherited Annotation is marked at the class level, the subclass can inherit the Annotation at the parent class level

We usually use custom annotations and add the following two

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
public @interface MyAnnotation {
    // When there is no default value, you need to add a value when using
    // Single member variable
    String value() default "tbc";
   // String[] value() default "tbc"; //  Multiple member variables

}

Topics: Java