Java annotations are essentially tags used to provide Meta data for your Java code. These are extra details that can be utilised by the java compiler or JVM. Being Meta data, the annotations do not directly affect the execution of your code, although some types of annotations can actually be used for that purpose. Java annotations were added to Java from Java 5.
Java annotations are typically used for the following purposes:
Java has three built-in annotations that you can use to give instructions to the Java compiler. These annotations are explained in more detail later in this chapter.
Java annotations can be used at build-time, when you build your software project. The building process includes generating source code, compiling the source, generating XML files (e.g. deployment descriptors), packaging the compiled code and files into a JAR file, etc.
Building the software is typically done by an automatic build tool like Apache Ant or Apache Maven. Build tools may scan your Java code for specific annotations and generate source code or other files based on these annotations.
Normally, Java annotations are not present in your Java code after compilation. It is possible, however, to define your own annotations that are available at runtime.
These annotations can then be accessed via Java Reflection, and used to give instructions to your program, or some third party API.
An annotation in its shortest form looks like this:
@SetValueJbk
The @ character signals to the compiler that this is an annotation. The name that follows the @character is the name of the annotation.
@SetValueJbk(value = "Kiran JBK") String name;
The annotation in this example contains a single element named name, with the value set to Kiran JBK. Here, the elements are enclosed inside the parentheses after the annotation name. Annotations without elements do not need parentheses.
You can put Java annotations above classes, interfaces, methods, method parameters, fields and local variables.
1) SetValueJbk.java
package com.javabykiran.Annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @authorKiran
* This annotation is used for
* setting values to variables in a class
*/
@Retentation(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public@interface SetValueJbk {
public String value ();
}
2) Student.java
package com.javabykiran.Annotations;
public class Student{
@SetValueJbk(value = “30”)
int age;
@SetValueJbk(value = "Pune")
String location;
@SetValueJbk(value = "Kiran JBK")
String name;
@SetValueJbk(value = "Java By Kiran")
String Owner_of;
}
3) AnnotTest.java
package com.javabykiran.Annotations;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
public classAnnotTest{
public static void main(String[] args){
/*
* we will get parameters for student class like values for age and location. Then we can use this class anywhere
* for example,to insert into DB
*/
Class aclass = Student.class;
Field field[] = aclass.getDeclaredFields();
for (Field field : fields) {
system.out.println(“field name >>”+ field.getname());
Annotation annotation[] = field.getDeclaredAnnotations();
for(Annotation annotation: annotations){
SetValueJbk annotRef = (SetValueJbk)annotation;
system.out.println(“name :” +annotRef.value());
}
}
}
}
If there is NO annotation, then we need to set and get values and dynamic usage is not possible. The same thing is internally implemented in Hibernate, which we do by using annotations.
Java comes with three annotations which are used to give the Java compiler instructions.
These annotations are:
Each of these annotations is explained in the following sections:
The @Deprecated annotation is used to mark a class, method or field as deprecated, meaning it should no longer be used. If your code uses deprecated classes, methods or fields, the compiler will give you a warning.
Here is an example:
@Deprecated
public class MyComponent {
}
The use of the @Deprecated annotation above the class declaration marks the class as deprecated.
You can also use the @Deprecated annotation like the above method and field declarations to mark the method or field as deprecated. When you use the @Deprecated annotation, it is a good idea to also use the corresponding @deprecated JavaDoc symbol, and explain why the class, method or field is deprecated, and what the programmer should use instead.
The @Override annotation is used above methods that override methods in a superclass. If the method does not match a method in the superclass, the compiler will give you an error. It is one of the reasons why it is considered to be a good practice while coding in java.
The @Override annotation is not necessary in order to override a method in a superclass. It is a good idea to still use it though. In case someone changed the name of the overridden method in the superclass, your subclass method would no longer override it.
Without the @Override annotation you would not find out what happened.
With the @Override annotation, the compiler will tell you that the method in the subclass is not overriding any method in the superclass.
Here is an example use of the @Override annotation:
public class A{
public void m1() {
System.out.println("A-m1");
}
}
public class B extends A{
@Override
public void m1() {
System.out.println("B-m1");
}
}
In case the method m1() in A changes signature so that the same method in the subclass no longer overrides it, the compiler will generate an error.
The @Suppress Warnings annotation makes the compiler suppress warnings for a given method or the annotated element. For instance, if a method calls a deprecated method, or makes an sinsecure type cast, the compiler may generate a warning.
You can suppress these warnings by annotating the method containing the code with the @SuppressWarnings annotation. For example:
@SuppressWarnings
public void methodWithWarning() {
}