Sitemap
Cathrine Peterraj

Cathrine Peterraj

Custom Annotations

4 min readNov 8, 2025
Press enter or click to view image in full size

What is an Annotation?

Java Annotation is metadata about the source code. Annotation is metadata that’s can be related to a class, interface, methods, and fields used by JVM. In simple terms , java annotations are special note or tag we apply to our java code which provide extra information and it doesn’t affect functionality.

These are the following properties one should know about Annotations:

Annotations are an alternative to XML code.

Annotations always start with “@” followed by the Annotation name.

Annotations can not change the action of a compiled program.

There are some restrictions on annotations one should know

We can’t inherit an Annotation.

Annotation methods do not have arguments.

Annotation methods should not have a throws clause.

Annotation Method should return one of the following: primitive data types, String, Class, Enum, or array of these data types.

Types of Annotation

Press enter or click to view image in full size

Annotations in Spring Boot allow us to encapsulate reusable logic in a simple and structured way. While Spring Boot provides several built-in annotations, it also allows us to create custom annotations to enhance code modularity and reduce redundancy. Specifically In this Blog i Discuss about when we used Custom Annotations and how to create the custom annotation. custom annotations can be applied to various elements like classes, methods, fields and interfaces.

Steps to create a Custom annotation:

  1. Create a Annotation class

If you want to create a own annotation first you need to create a interface file. For creating annotation we need 3 main annotations.

@Target ,@Retention ,@Interface

@Target: Its helps to specify the java element in which type.

Get Cathrine Peterraj’s stories in your inbox

Join Medium for free to get updates from this writer.

Element Type(Class) — ->ElementType.TYPE. The Type is show on the top of the java code , but if you want to add same annotation and different various elements so we need to use {…} and put @Target({ElementType.TYPE, ElementType.METHOD}).

@Retention: This annotation was used in when our Own annotation to the program.

  1. RUNTIME : In our own annotation was in the program its used while program Runtime. Mostly we used This Retention policy.
  2. SOURCE: We can also use this annotation in Retention annotation for policy. Our own annotation will starts before compile your code. so we can use this Source.
  3. CLASS: The class is when the annotation starts through the program & when it compile and Running is automatically grid of this.

@Interface: This annotation is the main in Custom annotation. This annotation has 3 types , that's based on the Elements present in the Annotation class,

  1. Marker annotation without any elements in @interface annotation

2.Single Value Annotation

3.Multi Value Annotation

Step 2: Then Create a Java class for Testing the Annotation is Working or not. For Different Elements We can create a different Custom annotation and checked it work properly or not.

  1. Class
  2. Method
  3. Field
Press enter or click to view image in full size

Custom Annotation We Created

@isAnnotationPresent: This Annotation was used for Checking Our Custom annotation is Working or not. For Method Element We need to use GetDeclareds(), Also the Field Element We need to use Get Fields().

In this annotation when i use GetDeclared(), GetField() that makes Some Error. because we need to use to get GetDeclareds(), Get Fields().

Press enter or click to view image in full size

--

--