Lesson 23: Annotations and Reflection

Yakov Fain · 2015

There are a number of predefined annotations (for example, @Override and @SuppressWarning). The Java annotation model enables you to add custom metadata anywhere in your code. The subject of Java reflection doesn't require the use of annotations; reflection is used widely in various areas of Java development. Annotations with the CLASS retention policy stay in the compiled class, but are not loaded during run time. The CLASS retention policy is used by default if a retention policy is not explicitly specified. Annotations with the RUNTIME retention policy have to be processed by a custom processing tool when the compiled code is running. Reflection enables one to find out about the internals of a Java class during the run time, and to invoke the discovered methods or access public member variables. Developers add the annotation to their classes and compile them, and the processing tool consumes these classes during run time.

Read the paper · More papers on PaperTik