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:
- Annotations always start with a ‘@’. For example: ‘@override’, ‘@target’, ‘@SuppressWarnings’, etc.
- Annotations do not affect the behaviour of a compiled program.
- Annotations aid in associating metadata to program components like methods, classes, variables, etc.
- 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”)})
Explore our Popular Software Engineering Courses
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!
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.
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.
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.