Javaa Foundation--Enumerations and Annotations


  • Introduction to Enumeration:
    Enumeration is also a class and has all its members. Using enumeration types ensures the "limitations", "security" and "correctness" of data information.

    1. Limitation: When an enumeration is defined, it can define a specified range. When an enumeration type is used, or when an enumeration type is used to assign a value, the attributes in the assigned object cannot send changes unless they are sent changes by member attributes within the object. Nevertheless, the defined enumeration type has no effect.
    2. Security: Enumeration type members are defined in entity types, so they can only use the range of values defined in the enumeration type, which ensures that data interaction rejects the incoming illegal data and cannot be changed arbitrarily. (e.g., defining gender types)
    3. Correctness: Ensure that the data is correct when it is compiled. (See requirements to define the enumeration content to effectively ensure it).
  • Use scenarios:
    --- Values that are deterministic in reality, such as defining gender, defining anomalies, defining provincial addresses of nationality, etc. When defining enumeration, try not to choose a definition that is as broad, variable, and random as possible.

*Define a gender enumeration class
public enum Sex {
    public int age;
    Sex(int age){
        this.age = age;

    public void showAge(){
        System.out.println("age is:" +age);

*Value Access Specifically See Entity Stu Definition
public class TestEnum {
    public static void main(String[] args) {
        Stu stu = new Stu("stu",Sex.GIRL,15);
        Stu stu1 = new Stu("stu1",Sex.GIRL,17);
        //Age members in stu2 use the range of values in the enumeration. Since the types defined by age members in stu2 entity classes are identical, the range of values for the enumeration type can also be used.
        Stu stu2 = new Stu("stu2",Sex.BOY,Sex.GIRL.age);
        Stu stu3 = new Stu("stu2",Sex.BOY);
        System.out.println(stu); //age is 15
        System.out.println(stu1); //age is 17
        System.out.println(stu2); //age is 16
        System.out.println(stu3); //age is 0
class Stu{
    private String name;
    private Sex sex;  //Define gender using enumeration type
    private int age;  //Compile-time errors require consistency with member types of entity classes, pass or error.
    public Stu() {

    public int getAge() {
        return age;

    public void setAge(int age) {
  //      this.age = sex.age;This block does not affect the value
        this.age = age;

    public Stu(String name, Sex sex, int age) { = name; = sex;
        this.age = age;

    public Stu(String name, Sex sex) { = name; = sex;
     //        this.age = sex.age;This block directly affects the value

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public Sex getSex() {
        return sex;

    public void setSex(Sex sex) { = sex;

    public String toString() {
        return "Stu{" +
                "name='" + name + '\'' +
                ", sex=" + sex +
                ", age=" + age +


  • Note Introduction:

    1. Annotations are a new feature of JDK1.5
    2. Annotations are equivalent to a tag and are part of a class that can carry some extra information to it.
    3. Annotations are added to packages, classes, fields, methods, method parameters, and local variables.
    4. Annotations are for the compiler or jvm to see, which can be used by the compiler or jvm to complete the corresponding functions.
  • meta annotation

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

 * Customize Student Notes
public @interface StuTestAnnotation{
    String name() default " ";
    int age() default 0;
    char sex() default ' ';

@StuTestAnnotation(name = "stuClass",age = 15,sex = 'male')
public class StudentOfAnnotation {

    @StuTestAnnotation(name = "stuMethod",age = 16,sex = 'female')
    public void stuTestAnnotation(){


import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;

public class StudentMain {

     * isAnnotationPresent:Determine whether the object has a specified comment
     * getAnnotation:Getting corresponding annotation objects based on annotation types
     * getDeclaredAnnotations:Gets all the annotations used on the current object and returns an array of annotations, including those inherited by the parent
     * @param args
    public static void main(String[] args) {
        //Get Object
        Class student = StudentOfAnnotation.class;
        //Determine whether the StuTestAnnotation annotation is used on a class
        System.out.println("Get on Class:");
        if (student.isAnnotationPresent(StuTestAnnotation.class)){
            //Obtain the corresponding annotation object from the annotation class object
            StuTestAnnotation annotation =(StuTestAnnotation) student.getAnnotation(StuTestAnnotation.class);
        //Gets all the annotations used on the current object, and returns the annotation array
        Annotation[] declaredAnnotations = student.getDeclaredAnnotations();

        System.out.println("On acquisition method:");
        try {
            Method method = student.getMethod("stuTestAnnotation");
            StuTestAnnotation annotation = method.getAnnotation(StuTestAnnotation.class);
            Annotation[] declaredAnnotations1 = method.getDeclaredAnnotations();
        } catch (NoSuchMethodException e) {

Tags: Java

Posted on Mon, 11 Oct 2021 12:32:09 -0400 by tuneout