Single Value Annotations in Java

by | Apr 16, 2021 | Java

Home » Java » Single Value Annotations in Java

Single Value Annotations in Java

Single Value Annotations, also commonly referred to as Single Member Annotations, has only one member. What makes single value annotations different from normal annotations is that these annotations allow a shorthand form to specify or state a certain value of the single member. As this type of annotation has only one member, you do not require to specify the name of the member when initializing or specifying a certain value for it. It is understood that the value of the only member will be set. In case you are using the shorthand for this type of annotation, you will need to name the member ‘value’.

Sample Code

//The following code is a sample code to show the usage of Java Single Member Annotation. In this code snippet, a single member annotation will be created and used
import java.lang.annotation.*;
import java.lang.reflect.*;
@Retention (RetentionPolicy.RUNTIME)
@interface MySingle {
int value ()
class Single
@MySingle (10)
public static void myMethod()
Single obj = new Single ();
Method m = obj.getClass().getMethod (“myMethod”);
MySingle anno = m.getAnnotation (MySingle.class);
System.out.println (anno.value ());
catch (NoSuchMethodException exc)
System.out.println (“Method not found !!”);
public static void main (String args [])
myMethod ();




Explanation of Output

As expected from the program, the output is 10. This is because the @MySingle annotation annotates the myMethod() method using: @MySingle(10).

As there is only one member, the member’s name has not been specified. Single value annotations can also be used to annotate in cases where there are other members. However, all the members must have default names.


@interface SomeAnnotate {
int value;
int abc() default 0;



Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.