Amity School of Engineering & Technology (CSE)
Annotations
Annotations Amity School of Engineering & Technology (CSE)
• Annotations are metadata or special markers that
provide information about a program but do not directly
affect its execution.
• They are used to convey instructions to compilers,
tools, or frameworks for various purposes such as code
documentation, runtime processing, or validation.
Amity School of Engineering & Technology (CSE)
Annotations in Java
• Java annotations provide metadata about the code and
are used for:
– Compilation checks
– Code generation
– Runtime processing by frameworks
Amity School of Engineering & Technology (CSE)
Built-In Java Annotations @Override: assures that the
subclass method is overriding the
used in Java code parent class method. If it is not so,
compile time error occurs.
@SuppressWarnings: used to
suppress warnings issued by the
compiler.
@Deprecated: It informs user that it
may be removed in the future
versions. So, it is better not to use
such methods.
Amity School of Engineering & Technology (CSE)
Example of @Override
public class Student { public class Annotation_Demo extends Student{
@Override
String name="Supriya"; public void display(){
String roll="1234"; System.out.println("Override example");
}
public void display(){ public static void main(String args[]){
System.out.println(name + " Annotation_Demo obj=new
"+roll); Annotation_Demo();
obj.display();
}
}
} }
Amity School of Engineering & Technology (CSE)
Example of @Deprecated
public class calc { public static void main(String args[])
@Deprecated {
public int add(int a, int b)
calc obj=new calc();
{
return(a+b); int b= obj.add(3,4,5);
} System.out.println(b);
public int add(int...num) }
{ }
int sum=0;
for(int i:num)
sum=sum+i;
return (sum);
}
Amity School of Engineering & Technology (CSE)
Example of @SuppressWarnings
class Student{
String name;
Student(String name){
this.name=name;}
}
class Main{
public static void main(String []args){
@SuppressWarnings(“unused”)
Student stud=new Student(“AAA”);
}
}
Amity School of Engineering & Technology (CSE)
Example of @FunctionalInterface
@FunctionalInterface
interface I1{
void display();
}
public class funinterface implements
I1{
public void display(){
}
Meta-Annotations Amity School of Engineering & Technology (CSE)
• Java provides meta-annotations that define how
annotations are applied.
– @Retention
– @Target
– @Documented
– @Inherited
@Retention Amity School of Engineering & Technology (CSE)
• Specifies how long an annotation should be retained.
– RetentionPolicy.SOURCE → Discarded during compilation
– RetentionPolicy.CLASS → Present in the class file but not
available at runtime
– RetentionPolicy.RUNTIME → Available during runtime
Example Amity School of Engineering & Technology (CSE)
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
String value();
}
@Target Amity School of Engineering & Technology (CSE)
Specifies where the annotation can be applied, such as
methods, fields, or classes.
Example Amity School of Engineering & Technology (CSE)
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target(ElementType.METHOD) // Can only be applied
to methods
@interface MethodOnlyAnnotation {
}
@Documented Amity School of Engineering & Technology (CSE)
• Indicates that the annotation should be included in the
JavaDocs.
import java.lang.annotation.Documented;
@Documented
@interface DocumentedAnnotation {
}
@Inherited Amity School of Engineering & Technology (CSE)
• Allows a subclass to inherit an annotation from its
superclass
import java.lang.annotation.Inherited;
@Inherited
@interface InheritableAnnotation {
}
Amity School of Engineering & Technology (CSE)
Types of Annotations
1. Marker Annotation: An annotation that has no method.
@interface MyAnnotation{ }
2. Single-Value Annotation: An annotation that has one method.
@interface MyAnnotation{
int value();
}
3. Multi-Value Annotation: An annotation that has more than one methods.
@interface MyAnnotation{
int value1();
String value2();
String value3();
}
}
Amity School of Engineering & Technology (CSE)
Custom Annotation
The @interface element is used to declare an annotation. @interface
MyAnnotation{ }
Method should not have any throws clauses.
Method should return one of the following: primitive data types, String,
Class, enum or array of these data types.
We should attach @ just before interface keyword
Method should not have any to define annotation.
parameter. It may assign a default value to the method.
Example Amity School of Engineering & Technology (CSE)
//Very.Important.java
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface VeryImportant {
}
Contd… Amity School of Engineering & Technology (CSE)
@VeryImportant
class student{
String name;
String roll;
public student(String name, String roll) {
this.name = name;
this.roll = roll;
}
public void display(){
System.out.println(name+ " "+roll);
}
}
Contd… Amity School of Engineering & Technology (CSE)
public class Main {
public static void main(String[] args) {
student stud=new student("aaa","1234");
if(stud.getClass().isAnnotationPresent(VeryImportant.class)) {
System.out.println("student class is very important");
}
else{
System.out.println("not so important");
}
}}
Example-2 Amity School of Engineering & Technology (CSE)
//runimmediately.java
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface runimmediately {
}
Contd… Amity School of Engineering & Technology (CSE)
class student{
String name;
String roll;
public student(String name, String roll) {
this.name = name;
this.roll = roll;
}
@runimmediately
public void display(){
System.out.println(name+ " "+roll);
}
}
Contd… Amity School of Engineering & Technology (CSE)
public class Main {
public static void main(String[] args) throws InvocationTargetException,
IllegalAccessException {
student stud=new student("aaa","1234");
for(Method method:stud.getClass().getDeclaredMethods()){
if(method.isAnnotationPresent(runimmediately.class)){
method.invoke(stud);
}
}
}}
Example 3 Amity School of Engineering & Technology (CSE)
//imp.java
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface imp {
String value() default "sss";
}
Contd… Amity School of Engineering & Technology (CSE)
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class student{
@imp
String name;
@runimmediately
void display(){
System.out.println(name);
}
}
Contd… Amity School of Engineering & Technology (CSE)
public class Main {
public static void main(String[] args) throws Exception {
student s=new student();
for (Field field:s.getClass().getDeclaredFields()){
if(field.isAnnotationPresent(imp.class)){
imp annotation=field.getAnnotation(imp.class);
System.out.println(field.getName() + " "+ annotation.value());
}
}
}
}
Amity School of Engineering & Technology (CSE)
Example-3
@interface Smartphone public class AnotationDemo
{ {
String os() default "Android"; public static void main(String args[])
int ver() default 1; {
} Nokia obj=new Nokia();
@Smartphone (os="Android", ver=6) }
class Nokia{
public void display(){ }
System.out.println("Annotation Smartphone");
}
}
Amity School of Engineering & Technology (CSE)
Creating, applying and accessing annotation
@Retention(RetentionPolicy.RUNTI class Nokia{
ME) @SmartPhone(os="android")
@Target(ElementType.METHOD) public void display()
@interface SmartPhone{ {
String os() default "Windows"; System.out.println("Annotation
int ver() default 1; Smartphone");
} }
}
Amity School of Engineering & Technology (CSE)
public class AnnotationDemo{
public static void main(String[] args) throws Exception {
Nokia obj=new Nokia();
Method m=obj.getClass().getMethod("display");
SmartPhone s=m.getAnnotation(SmartPhone.class);
System.out.println("OS is: "+s.os() + " and "+"Version is: "+s.ver());
}
}
Amity School of Engineering & Technology (CSE)
Creating, applying and accessing annotation
@Retention(RetentionPolicy.RUNTI class Nokia{
ME) @SmartPhone(os="android“, ver=8)
@Target(ElementType.METHOD) public void display()
@interface SmartPhone{ {
String os() default "Windows"; System.out.println("Annotation
int ver() default 1; Smartphone");
} }
}