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"; [Link]("Override example");
}
public void display(){ public static void main(String args[]){
[Link](name + " Annotation_Demo obj=new
"+roll); Annotation_Demo();
[Link]();
}
}
} }
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= [Link](3,4,5);
} [Link](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){
[Link]=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.
– [Link] → Discarded during compilation
– [Link] → Present in the class file but not
available at runtime
– [Link] → Available during runtime
Example Amity School of Engineering & Technology (CSE)
import [Link];
import [Link];
@Retention([Link])
@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 [Link];
import [Link];
@Target([Link]) // 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 [Link];
@Documented
@interface DocumentedAnnotation {
}
@Inherited Amity School of Engineering & Technology (CSE)
• Allows a subclass to inherit an annotation from its
superclass
import [Link];
@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)
//[Link]
import [Link];
import [Link];
import [Link];
import [Link];
@Target([Link])
@Retention([Link])
public @interface VeryImportant {
}
Contd… Amity School of Engineering & Technology (CSE)
@VeryImportant
class student{
String name;
String roll;
public student(String name, String roll) {
[Link] = name;
[Link] = roll;
}
public void display(){
[Link](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([Link]().isAnnotationPresent([Link])) {
[Link]("student class is very important");
}
else{
[Link]("not so important");
}
}}
Example-2 Amity School of Engineering & Technology (CSE)
//[Link]
@Target([Link])
@Retention([Link])
public @interface runimmediately {
}
Contd… Amity School of Engineering & Technology (CSE)
class student{
String name;
String roll;
public student(String name, String roll) {
[Link] = name;
[Link] = roll;
}
@runimmediately
public void display(){
[Link](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:[Link]().getDeclaredMethods()){
if([Link]([Link])){
[Link](stud);
}
}
}}
Example 3 Amity School of Engineering & Technology (CSE)
//[Link]
import [Link];
import [Link];
import [Link];
import [Link];
@Target([Link])
@Retention([Link])
public @interface imp {
String value() default "sss";
}
Contd… Amity School of Engineering & Technology (CSE)
import [Link];
import [Link];
import [Link];
class student{
@imp
String name;
@runimmediately
void display(){
[Link](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:[Link]().getDeclaredFields()){
if([Link]([Link])){
imp annotation=[Link]([Link]);
[Link]([Link]() + " "+ [Link]());
}
}
}
}
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(){ }
[Link]("Annotation Smartphone");
}
}
Amity School of Engineering & Technology (CSE)
Creating, applying and accessing annotation
@Retention([Link] class Nokia{
ME) @SmartPhone(os="android")
@Target([Link]) public void display()
@interface SmartPhone{ {
String os() default "Windows"; [Link]("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=[Link]().getMethod("display");
SmartPhone s=[Link]([Link]);
[Link]("OS is: "+[Link]() + " and "+"Version is: "+[Link]());
}
}
Amity School of Engineering & Technology (CSE)
Creating, applying and accessing annotation
@Retention([Link] class Nokia{
ME) @SmartPhone(os="android“, ver=8)
@Target([Link]) public void display()
@interface SmartPhone{ {
String os() default "Windows"; [Link]("Annotation
int ver() default 1; Smartphone");
} }
}