For example the below annotation is being applied to the method. symbol represents that this is an annotation and the Override is the name of this annotation.Īnnotations can be applied to the classes, interfaces, methods and fields. The symbol indicates to the compiler that this is an annotation.įor e.g. Being meta data, the annotations do not directly affect the execution. Annotations basicsĪn annotation always starts with the symbol followed by the annotation name. Java annotations are used to provide meta data for your Java code. We will discuss this with the help of an example, later in this same post. More about these built-in annotations with example is discussed in the next sections of this article.Ģ) Compile-time instructors: Annotations can provide compile-time instructions to the compiler that can be further used by sofware build tools for generating code, XML files etc.ģ) Runtime instructions: We can define annotations to be available at runtime which we can access using java reflection and can be used to give instructions to the program at runtime. For example the annotation is used for instructing compiler that the annotated method is overriding the method.
What’s the use of Annotations?ġ) Instructions to the compiler: There are three built-in annotations available in Java ( & that can be used for giving certain instructions to the compiler. This one makes the compiler to validate the annotated class and check if it is.
#ANNOTATIONS IN JAVA HOW TO#
In this tutorial we are going to cover following topics: Usage of annotations, how to apply annotations, what predefined annotation types are available in the Java and how to create custom annotations. One example of this kind of usage is the Java 8 FunctionalInterface annotation. it does not affect the execution of the program).
Annotation has no direct effect on the operation of the code they annotate (i.e. Annotations were added to the java from JDK 5. Java Annotations allow us to add metadata information into our source code, although they are not a part of the program itself.