Module 5 Annotations
Module 5 Annotations
Annotations
Annotations 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)
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");
}
} }
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)
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
String value();
}
@Target Amity School of Engineering & Technology (CSE)
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.lang.annotation.Documented;
@Documented
@interface DocumentedAnnotation {
}
@Inherited Amity School of Engineering & Technology (CSE)
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{ }
Custom Annotation
The @interface element is used to declare an annotation. @interface
MyAnnotation{ }
Method should return one of the following: primitive data types, String,
Class, enum or array of these data types.
//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)
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;
}
Contd… 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)
}
}
Amity School of Engineering & Technology (CSE)