Annotations
Annotations
Note: When using deprecated methods from standard libraries, the compiler automatically issues a warning.
However, for custom or user-defined deprecated methods, you must explicitly mark them with the @Deprecated
annotation; otherwise, the compiler won't provide any warnings.
Ex:
import java.awt.*; /*
class TestDepricated{ D:\javaprogs>javac TestDepricated.java
public static void main(String [] args){ Note: TestDepricated.java uses or overrides a
Frame f=new Frame(); deprecated API.
f.show();// alternative is f.setVisible(); Note: Recompile with -Xlint:deprecation for
details.
}
*/
}
// User defined Deprecated methods
class Employee{
@Deprecated
public void gen_salary(){
System.out.println("BASIC+HRA");
}
public void new_Gen_salary(){
System.out.println("BASIC+DA+HRA");
} Output:
D:\javaprogs>javac TestDepricated.java
}
Note: TestDepricated.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
class TestDepricatedAnnotation{
public static void main(String []args){
Employee e=new Employee();
e.gen_salary();
}}
@FunctionalInterface
A functional interface is an interface that contains exactly one abstract
method.
Using @FunctionalInterface, clearly indicates that the interface is
meant to have only one abstract method. It also makes sure the
compiler enforces this rule by showing an error if there is more than
one abstract method.
.
@FunctionalInterface
interface A {
void x();
void y();// Extra abstract method, sothat compiler gives error
}
class B implements A{
public void x(){System.out.println("hi");}
public void y(){System.out.println("hello");}
}
class FunctionalInterfaceAnnotation{
public static void main(String[] args) { Output:
D:\javaprogs>javac FunctionalInterfaceAnnotation.java
A a=new B();
FunctionalInterfaceAnnotation.java:1: error: Unexpected
a.x(); @FunctionalInterface annotation
a.y(); @FunctionalInterface
} ^
A is not a functional interface
} multiple non-overriding abstract methods found in interface A
1 error
Java Custom Annotations
• @interface element is used to declare an annotation.
Syntax:
public @interface AnnotationName {
DataType elementName() default defaultValue;
}
Here,
•@interface : Keyword used to define an annotation.
•AnnotationName : The name of the custom annotation.
•DataType : The return type of the annotation element (like String, int,
boolean, Class, etc.). It can also be an array of any type (e.g., String[]).
•elementName() : Name of the element (like a method declaration). These are the
values you can pass to the annotation when you use it.
•defaultValue : (Optional) Default value for the annotation element. If no value is
provided by the user when applying the annotation, this default
will be used.
Categories of Annotations:
There are three Categories of annotations.
• Marker Annotation
• Single-Value Annotation
• Multi-Value Annotation
Marker Annotation
• An annotation that has no method, is called marker annotation.
Syntax:
@interface MyAnnotation{}
Ex:
• @Override
• @Deprecated
import java.lang.annotation.Retention; example.display();
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method; // Getting the class object
Class<?> clazz = example.getClass();
// Defining a marker annotation
@Retention(RetentionPolicy.RUNTIME) // Accessing the method by name
@interface TestMarker { try {
Method method = clazz.getMethod("display");
// No elements
}
// Checking if the annotation is present on the method
if (method.isAnnotationPresent(TestMarker.class))
// Applying marker annotation {
public class MarkerAnnotationExample { System.out.println("TestMarker annotation is present.");
} else {
@TestMarker System.out.println("TestMarker annotation is NOT
public void display() { present.");
System.out.println("Marker annotation }
applied!"); } catch (NoSuchMethodException e) {
} System.out.println("Method not found: " +
e.getMessage());
}
public static void main(String[] args) {
}
MarkerAnnotationExample example = new
}
Single-Value Annotation
An annotation that has one method, is called single-value annotation.
Syntax:
@interface MyAnnotation{
int value();
}
• Here we can provide the default value also.
For example:
@interface MyAnnotation{
int value() default 0;
}
import java.lang.annotation.Retention; // Retrieve the Author annotation
import java.lang.annotation.RetentionPolicy; Author authorAnnotation =
import java.lang.reflect.Method; method.getAnnotation(Author.class);
// Define a single-value annotation // Use the annotation value inside the method
@Retention(RetentionPolicy.RUNTIME) if (authorAnnotation != null) {
@interface Author { System.out.println("Feature one implemented by:
" + authorAnnotation.value());
String value(); // Single element
}
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
public class AuthorInfo {
}
}
@Author("Alice")
public void featureOne() {
public static void main(String[] args) {
// Use reflection to get the annotation value at runtime
AuthorInfo obj = new AuthorInfo();
try {
obj.featureOne();
Method method =
this.getClass().getMethod("featureOne"); }
}
Multi-Value Annotation
An annotation that has more than one method, is called Multi-Value annotation. For example:
@interface MyAnnotation{
int value1();
String value2();
String value3();
}
We can provide the default value also. For example:
@interface MyAnnotation{
int value1() default 1;
String value2() default "";
String value3() default "xyz";
}
How to apply Multi-Value Annotation
Let's see the code to apply the multi-value annotation.
@MyAnnotation(value1=10,value2="Arun Kumar",value3="Ghaziabad")
import java.lang.annotation.Retention; MultiValueExample obj = new MultiValueExample();
import java.lang.annotation.RetentionPolicy; obj.featureOne();
obj.featureTwo();
// Define a multi-value annotation
@Retention(RetentionPolicy.RUNTIME) // Using reflection to access annotation values
@interface DeveloperInfo { try {
String developer(); // One element for developer name // For the featureOne method
String date(); // Another element for development date DeveloperInfo featureOneInfo =
} obj.getClass().getMethod("featureOne").getAnnotation(Devel
operInfo.class);
System.out.println("Feature One - Developer: " +
// Apply the multi-value annotation featureOneInfo.developer() + ", Date: " +
public class MultiValueExample { featureOneInfo.date());
RetentionPolicy Availability
refers to the .class file, available to java compiler but not to JVM . It is
RetentionPolicy.CLASS
included in the class file.