What is it possible to use your own annotation in java? What practical application is possible for a class that is annoyed by the annotation created?

@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface Question { String data(); int data2(); } @Question(data = "What Next?", data2 = 1) public class MyObj { private String name; private Integer id; public MyObj(String name, Integer id) { this.name = name; this.id = id; } public String getName() { return name; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof MyObj)) return false; MyObj myObj = (MyObj) o; return id.equals(myObj.id); } @Override public int hashCode() { return id.hashCode(); } } 

It turns out that through reflection it is possible to obtain the values ​​recorded in the annotation for a particular class.

And what practical application of this information is possible?

    1 answer 1

    In fact, the application is limited only by your imagination: in the annotations you can write tests, describe the rules of caching, validation or other parameters of objects, control their behavior ...

    You just need to understand that since you yourself come up with these annotations, the tools for processing them will also need to be written to you.