Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Annotations in Java: Types, Uses and Examples

Updated on 03 July, 2023

6.1K+ views
9 min read

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:

  1. Annotations always start with a ‘@’. For example: ‘@override’, ‘@target’, ‘@SuppressWarnings’, etc.
  2. Annotations do not affect the behaviour of a compiled program.
  3. Annotations aid in associating metadata to program components like methods, classes, variables, etc.
  4. Annotations are not just comments since they can affect how a program is processed by the compiler. They offer additional compiler information about the program but are not part of the program itself. Therefore, these annotations do not affect the compiled program’s execution.

Types of Annotations in Java

1. Single Value Annotations

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”);

2. Full Annotations

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.

3. Repeating Annotations

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”)})

3. Marker Annotations

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.

4. Type annotations

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;
    }

Examples of Predefined Annotations

There are many varieties of predefined annotations which we have mentioned earlier. Let us take a look:

1. @Override

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()");
     }
}

2. @Target

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:

  • Source: The compiler disregards the annotations as they are usually kept at the source level.
  • Runtime: It is retained at runtime.
  • Class: The annotations remain compile-time and are disregarded by the Java program.

3. @Inherited

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

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:

  • @EnableAutoConfiguration: It helps in auto-configuring the bean available in the classpath. The purpose of configuring the classpath is to run different methods. The use of this annotation became limited with Spring Boot 1.2.0.
  • @SpringBootApplication: It can be defined as an alternative to the @EnableAutoConfiguration. Apart from that, this annotation also combines the properties of @Configuration and @ComponentScan. 

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;  
}  
} 

Custom Annotations in Java

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:

  • They can be created with @interface and the annotation name coming after it.
  • The annotation can include elements resembling methods but without any implementation.
  • The default value of custom annotations is optional. However, the parameters are not allowed to have a null value.
  • The return type can either be string, primitive, enum, array, or class name of the different types. 

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

Uses of Annotations in Java

The different uses of annotations in Java are as follows:

  • Compile-time instructions: They are provided by different annotations to help a software solution develop various tools. The tools are usually developed for generating code, XML files, or similar purposes. 
  • Compiler instructions: These annotations are useful for providing instructions to the compiler. They can also help with identifying errors and suppressing warnings. The popular built-in annotations used for these purposes include the @Override, @Deprecated, and @SuppressWarnings. 
  • Runtime Instructions: While providing instructions to a program during runtime, you can use multiple annotations in Java. But these annotations can only be accessed with the help of Java Reflection. 

Conclusion

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!

Frequently Asked Questions (FAQs)

1. What are @jsonproperty annotations?

Jsonproperty annotations allow developers to map JSON keys with property names during serialisation and deserialisation. Based on Java's default settings, keys are mapped to POJO fields during serialisation. Json annotations help override this default functionality by using string attributes that indicate the mapped name.

2. What are hibernate annotations?

Hibernate Annotations are another technique to specify metadata when mapping the Object and Relational Table. They are a powerful and relatively new mapping method that can be used in place of XML mapping metadata. Using hibernate annotations, users can store metadata in a single POJO java file that also carries the code, thereby helping to better understand a table structure.

3. What are @SuppressWarnings?

The @SuppressWarnings annotation notifies the compiler of warnings that are suppressed. The notifications are present in string form by name. It can be given to any kind of declaration. Java classifies warnings into two classes, namely unchecked and deprecated. When older code interacts with generics-based code, an unchecked warning is produced.

RELATED PROGRAMS