Defining and Using Annotations In Java

Defining and Using Annotations In Java

Annotations in the Java programming language are metadata detailing class information that provides detailed information about a user-defined custom class, class methods, class data type fields, variable parameters, etc. They were first introduced in Java version 5.

Defining and Using Annotations In Java

Remember, Java annotations do not directly modify the logic of a user-defined Java program, but they can be read by Java compilers, tools, or at runtime.

Examples of built-in annotations in the Java programming language.

  • @Override Java annotations
  • @Deprecated Java annotations
  • @SuppressWarnings Java annotations

Using built-in annotations in Java.

The @Override Java annotation indicates to the Java compiler that a user-defined class method is intended to override a parent-class method.

class Gadgets {

void laptop() {

System.out.println(“Gadgets is laptop”);

}

}

class Hp extends Gadgets {

@Override

void laptop() {

System.out.println(“Hp laptop”);

}

}

If Java users make any mistakes in the method signature here, the Java compiler can detect them.

@Deprecated annotations in Java.

The @Deprecated annotation indicates to the Java compiler that a user-defined custom class or method should no longer be used.

class Sample {

@Deprecated

void testMethod() {

System.out.println(“This is test method”);

}

}

Using the @Deprecated annotation in Java can cause compiler warnings to be displayed.

@SuppressWarnings annotations in Java.

The @SuppressWarnings annotation is used to tell the Java compiler to hide or suppress specific compiler warnings.

@SuppressWarnings(“random”)

void sample() {

// you can write your code

}

Creating a custom user-defined annotation in Java.

Java users can create their own custom annotation using the @interface annotation.

Syntax of a custom user-defined annotation.

@interface AnnotationName {

}

Example of a custom user-defined annotation.

@interface SampleAnnotation {

}

Using it in Java.

@SampleAnnotation

class Employee {

}

Annotations with elements in Java.

A user-defined Java annotation can contain elements that perform or function like variable parameters declared in a Java program.

@interface EmployeeDetail {

String emp_name();

int emp_age();

}

Using in elements.

@EmployeeDetail(emp_name = “Siddhi Deora”, emp_age = 19)

class Employee {

}

In this example.

emp_name = “Siddhi Deora”

emp_age = 19

Here, these are custom user-defined annotation values.

Default values ​​in Java annotations.

User-defined Java annotation elements can have default values.

@interface EmployeeDetail {

String emp_name();

int emp_age() default 23;

}

Now the Java user does not need to specify the age.

@EmployeeDetail(emp_name = “Bhavishi”)

class Employee {

}

Here, the default value of age in the Employee class will be 23.

@Retention annotations in Java.

The @Retention annotation indicates to the Java compiler how long the annotation is available.

There Are Three Retention Java Policies.

#@Retention PolicyDetail Meaning of @Retention annotation
1.SOURCEThe source policy only available in source code mode
2.CLASSThe class method stored in .class file but not necessarily available at runtime environment
3.RUNTIMEThis is available at runtime through reflection method

Example of @Retention annotations.

@Retention(RetentionPolicy.RUNTIME)

@interface TestAnnotation {

String value();

}

This is essential for runtime inspection in Java programs, for RUNTIME.

@Target annotations in Java.

The @Target annotation indicates to the Java compiler where the annotation can be used.

Example of @Target annotations.

@Target(ElementType.METHOD)

@interface TestAnnotation {

}

Now this can only be used on these class methods.

class Test {

@MyTestAnnotation

void preview() {

System.out.println(“Welcome”);

}

}

Common Java class annotations are included in the Element Type value.

  • TYPE → Class, interface
  • METHOD → Class Method
  • FIELD → Class Data Type Field
  • PARAMETER → Variable Parameter
  • CONSTRUCTOR → User Define Constructor
  • LOCAL_VARIABLE → Local Class variable

Reading Java annotations at runtime.

If an annotation uses RUNTIME retention, reflection can access it.

Annotation

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

@interface EmployeeDetail {

String emp_name();

int emp_age();

}

Using annotations in Java.

@EmployeeDetail(emp_name = “Harry”, emp_age = 27)

class Employee {

}

Reading this in Java.

Class<Employee> cls = Employee.class;

EmployeeDetail detail =

cls.getAnnotation(EmployeeDetail.class);

System.out.println(detail.emp_name());

System.out.println(detail.emp_age());

Its output.

Harry

27

Example of a complete Java annotation metadata.

import java.lang.annotation.*;

import java.lang.reflect.*;

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

@interface Employee {

String emp_name();

String doj();

}

@Employee(emp_name=”Bhavishi Deora”, doj=”2010-03-01″)

class Person {

}

public class Main {

public static void main(String[] args) {

Class<Person> cls = Person.class;

Employee employee =

cls.getAnnotation(Employee .class);

System.out.println(

“Employee name -: ” + employee.emp_name()

);

System.out.println(

“Date of joining -: ” + employee.doj()

);

}

}

Its output.

Employee name -: Bhavishi Deora

Date of joining -: 2010-03-01

Common java Meta-Annotations information.

The Java programming language provides its users with annotations that are used to define other annotations.

#Java Meta-AnnotationPurpose of multiple java annotation method
1.@Retention Annotation@Retention java annotation used to defines annotation for lifetime method
2.@Target Annotation@Target java annotation used to defines, where annotation can be used according to need
3.@Documented Annotation@Documented java annotation available o includes annotation in javadoc inbuilt
4.@Inherited Annotation@Inherited java annotation allows annotation inheritance by subclasses method
5.@Repeatable Annotation@Repeatable java annotation method allows an annotation to be used multiple times when user need

Flow of Simple Java Annotations

Define a Custom Annotation

↓

@interface SampleAnnotation

↓

Apply an annotation

↓

@SampleAnnotation

class Employee

↓

Read the annotation

↓

Reflection

↓

Detailed information about an annotation

Conclusion of Defining and Using Annotations.

  • Java annotations are detailed metadata information about elements in a Java program.
  • Custom Java annotations are created using the @interface method.
  • The @Target Java annotation provides control over where an annotation can be applied in the current program.
  • The @Retention Java annotation controls how long it is retained in a program.
  • The RUNTIME Java annotation method provides features for reading annotations using retention reflection.
  • Java annotations are used extensively in frameworks such as dependency injection, testing, and ORM systems.

Leave a Reply