Annotations in Java: Types, Uses and Examples
Updated on Jul 03, 2023 | 9 min read | 6.2k views
Share:
For working professionals
For fresh graduates
More
Updated on Jul 03, 2023 | 9 min read | 6.2k views
Share:
Table of Contents
An annotation in a programming language such as Java refers to the syntactic metadata that can be integrated with Java’s source code. Various factors such as Java packages, variables, classes, parameters and methods can be annotated. Developers may easily read the Java annotations directly from the source files, just like Javadoc tags.
Java annotations can also be included in and read from the Java compiler’s output Java class files. This enables the Java virtual machine to maintain annotations and read them through a reflection at run-time. In Java, it is possible to build meta-annotations from existing ones.
These are some basics of annotations in Java:
Single value annotations allow only one shorthand form since they contain only one member. The member must be given a value post-application of the annotation. However, the name of the annotation does not need to be specified. The member must have a value if the shorthand is to be used. For example:
@TestAnnotation(“testing”);
Full annotations consist of various data such as data members, values, pairs and names.
For example:
@TestAnnotation(owner=”Rahul”, value=”Class Geeks”)
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.
When an annotation can be applied to singular objects more than once, it is labelled as a repeating annotation. Repeating annotations are specified with the @Repeatable tag as defined in the java.lang.annotation package. Its value field indicates the repeated annotation’s container type.
The container is defined as an annotation with a value field that contains an array of repeating annotations. To construct a repeated annotation, first create the container annotation, and then specify the annotation type as an input to the @Repeatable annotation.
For example:
// Java Program to Demonstrate a Repeatable Annotation
// Importing required classes
import java.lang.annotation.Annotation;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
// Make Words annotation repeatable
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(MyRepeatedAnnos.class)
@interface Words
{
String word() default "Hello";
int value() default 0;
}
// Create container annotation
@Retention(RetentionPolicy.RUNTIME)
@interface MyRepeatedAnnos
{
Words[] value();
}
public class Main {
// Repeat Words on newMethod
@Words(word = "First", value = 1)
@Words(word = "Second", value = 2)
public static void newMethod()
{
Main obj = new Main();
try {
Class<?> c = obj.getClass();
// Obtain the annotation for newMethod
Method m = c.getMethod("newMethod");
// Display the repeated annotation
Annotation anno
= m.getAnnotation(MyRepeatedAnnos.class);
System.out.println(anno);
}
catch (NoSuchMethodException e) {
System.out.println(e);
}
}
public static void main(String[] args) { newMethod(); }
}
The final output would be: @MyRepeatedAnnos(value={@Words(value=1, word=”First”), @Words(value=2, word=”Second”)})
Marker annotations are only meant for declaration purposes. Neither does this annotation contain any members nor does any data exist within it. Its only purpose is to maintain a presence as an annotation. For example: @Override.
These annotations can be used everywhere that a type is used. For example, we can annotate a method’s return type. Type annotations are tagged with the @Target annotation.
Code which can illustrate type annotations are:
// Importing required classes
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
// Using target annotation to annotate a type
@Target(ElementType.TYPE_USE)
// Declaring a simple type annotation
@interface TypeAnnoDemo{}
// Main class
public class GFG {
// Main driver method
public static void main(String[] args) {
// Annotating the type of a string
@TypeAnnoDemo String string = "This code is annotated with a type annotation";
System.out.println(string);
abc();
}
// Annotating return type of a function
static @TypeAnnoDemo int abc() {
System.out.println("This function's return type is annotated");
return 0;
}
There are many varieties of predefined annotations which we have mentioned earlier. Let us take a look:
This marker annotation can only be used on methods. Any method with the @override annotation must supersede another method from a superclass. Failing this, a compile-time error gets triggered. This happens since it must ensure that the superclass is actually overridden and not just overloaded.
Code which can illustrate the Override Annotation is:
// Class 1
class Base
{
public void Display()
{
System.out.println("Base display()");
}
public static void main(String args[])
{
Base t1 = new Derived();
t1.Display();
}
}
// Class 2
// Extending above class
class Derived extends Base
{
@Override
public void Display()
{
System.out.println("Derived display()");
}
}
It is only meant to be used as an annotation to another annotation. @Target accepts one parameter, which must be an ElementType constant. The parameter decides which declarations it can be applied to. The @Target annotation can have multiple values instead of just a singular value. However, if multiple values must be declared, then it must be in the form of braces such as: @Target({ElementType.FIELD, ElementType.LOCAL_VARIABLE}).
The @Retention annotation can be used here to determine the retention status of the annotation. Three factors affect a @Retention annotation:
This annotation can only be used for declaration purposes, and hence, only affects certain class declarations. One annotation can supersede the other and inherit its properties. Thus, the superclass can be checked if specific annotations are absent from the subclass. However, if the annotation is present, it gets the @Inherited annotation.
For example:
// Class 1
class DeprecatedTest
{
@Deprecated
public void Display()
{
System.out.println("Deprecatedtest display()");
}
}
// Class 2
public class SuppressWarningTest
{
// If we comment below annotation, program generates
// warning
@SuppressWarnings({"checked", "deprecation"})
public static void main(String args[])
{
DeprecatedTest d1 = new DeprecatedTest()
d1.Display();
}
}
Spring Annotations in Java act as metadata and offer program data. These annotations are extremely useful for delivering supplemental program information. These annotations don’t belong to the application being developed. Therefore, it does not directly influence the operation of the code and can’t modify the action of the compiled program.
The different annotations in Spring Boot are as follows:
A few core Spring Annotations in Java are as follows:
@Required: It can help with the application of the bean setter method. It symbolizes that the annotated bean needs to be populated with the required property at the time of configuration. Otherwise, an exception called the BeanInitializationException will surface.
Examples of the @Required Annotation:
public class Machine
{
private Integer cost;
@Required
public void setCost(Integer cost)
{
this.cost = cost;
}
public Integer getCost()
{
return cost;
}
}
@Autowired: This is useful for supporting auto-wiring based on annotations in Java. It can help autowire spring beans on a constructor, instance variable, and setter methods. This annotation can lead to auto-wiring by matching the data types.
Example of the Autowired Annotation
@Component
public class Customer
{
private Person person;
@Autowired
public Customer(Person person)
{
this.person=person;
}
}
Java also lets you create your own annotations using the syntax:
[Access Specifier] @interface<AnnotationName> {
DataType <Method Name>() [default value];
}
A few crucial things to know about custom annotations in Java are as follows:
Example of Custom Annotation
@interface MyCustomAnnotation {
String value() default "default value";
}
class Main {
@MyCustomAnnotation(value = "programiz")
public void method1() {
System.out.println("Test method 1");
}
public static void main(String[] args) throws Exception {
Main obj = new Main();
obj.method1();
}
}
Output:
Test method 1
The different uses of annotations in Java are as follows:
Here, we have learned about annotations in Java. We have also learnt about the types of annotations in Java and their uses which can help Java developers create robust and scalable programs with ease.
If you’d like to learn more about Java concepts, upGrad’s Job-linked PG Certification in Software Engineering is your best bet to gain a comprehensive understanding of programming languages like Java, HTML, JS, as well as other software development tools and libraries.
The program is designed for final year students or unemployed graduates looking to secure entry-level positions in the field. The 5-months course covers specialisation in MERN/Cloud-Native and helps students build a top-class portfolio by exposing them to 5 hands-on projects.
So, don’t wait. Head over to upGrad and book your seat today!
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources