• Stack Overflow Public questions & answers
  • Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
  • Talent Build your employer brand
  • Advertising Reach developers & technologists worldwide
  • About the company

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Lists with Java Annotations

I want to validate my arguments with Java Annotation. I don't know how to use write a own Annotation for Lists.

Here an simple example:

my Annotation Interface:

In this Example my List is valid, if each element from my List has another name. I don't know how I get the listobject in my Validator class and the names of myObject-elements.

I try to describe my question in another way:

my list is not valid if two elements from type "myObject" in my list ("myElements") have the same name!

How could I realize this with Annotations?

ROMANIA_engineer's user avatar

2 Answers 2

In the initialize method you can get the values, which are defined in the annotation. The isValid method is used to validate the object (objectToValidate -> your list object).

For more information on how to write a custom validator see http://docs.jboss.org/hibernate/validator/4.3/reference/en-US/html/validator-customconstraints.html#validator-customconstraints-validator

Also the Hibernate-Validator implementation is a good reference. https://github.com/hibernate/hibernate-validator/tree/master/engine/src/main/java/org/hibernate/validator/internal/constraintvalidators

I hope this answer helps you.

matthias's user avatar

I can answer your question partly, you can get names out from the collection which is sent as parameter in your isValid method. Iterate your collection (which in your case a list).

Sajan Chandran's user avatar

Your Answer

Sign up or log in, post as a guest.

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service , privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged java annotations bean-validation or ask your own question .

Hot Network Questions

annotate list java

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .

Learn Java interactively.

Learn Java practically and Get Certified .

Popular Tutorials

Popular examples, reference materials.

Learn Java Interactively

Java Introduction

Java Flow Control

Java OOP (I)

Java OOP (II)

Java Inheritance

Java Method Overriding

Java OOP (III)

Java Annotations

Java Annotation Types

Java Reader/Writer

Additional Topics

Related Topics

Java Lambda Expressions

In this tutorial, we will learn about different types of Java annotation with the help of examples.

Java annotations are metadata (data about data) for our program source code. There are several predefined annotations provided by the Java SE. Moreover, we can also create custom annotations as per our needs.

If you do not know what annotations are, visit the Java annotations tutorial.

These annotations can be categorized as:

1. Predefined annotations

2. Custom annotations

3. Meta-annotations

1. @Deprecated

The @Deprecated annotation is a marker annotation that indicates the element (class, method, field, etc) is deprecated and has been replaced by a newer element.

Its syntax is:

When a program uses the element that has been declared deprecated, the compiler generates a warning.

We use Javadoc @deprecated tag for documenting the deprecated element.

Example 1: @Deprecated annotation example

2. @override.

The @Override annotation specifies that a method of a subclass overrides the method of the superclass with the same method name, return type, and parameter list.

It is not mandatory to use @Override when overriding a method. However, if we use it, the compiler gives an error if something is wrong (such as wrong parameter type) while overriding the method.

Example 2: @Override annotation example

In this example, by making an object dog1 of Dog class, we can call its method printMessage() which then executes the display() statement.

Since display() is defined in both the classes, the method of subclass Dog overrides the method of superclass Animal . Hence, the display() of the subclass is called.

3. @SuppressWarnings

As the name suggests, the @SuppressWarnings annotation instructs the compiler to suppress warnings that are generated while the program executes.

We can specify the type of warnings to be suppressed. The warnings that can be suppressed are compiler-specific but there are two categories of warnings: deprecation and unchecked .

To suppress a particular category of warning, we use:

For example,

To suppress multiple categories of warnings, we use:

Category deprecated instructs the compiler to suppress warnings when we use a deprecated element.

Category unchecked instructs the compiler to suppress warnings when we use raw types.

And, undefined warnings are ignored. For example,

Example 3: @SuppressWarnings annotation example

Here, deprecatedMethod() has been marked as deprecated and will give compiler warnings when used. By using the @SuppressWarnings("deprecated") annotation, we can avoid compiler warnings.

4. @SafeVarargs

The @SafeVarargs annotation asserts that the annotated method or constructor does not perform unsafe operations on its varargs (variable number of arguments).

We can only use this annotation on methods or constructors that cannot be overridden. This is because the methods that override them might perform unsafe operations.

Before Java 9, we could use this annotation only on final or static methods because they cannot be overridden. We can now use this annotation for private methods as well.

Example 4: @SafeVarargs annotation example

Here, List ... lists specifies a variable-length argument of type List . This means that the method displayList() can have zero or more arguments.

The above program compiles without errors but gives warnings when @SafeVarargs annotation isn't used.

When we use @SafeVarargs annotation in the above example,

We get the same output but without any warnings. Unchecked warnings are also suppressed when we use this annotation.

5. @FunctionalInterface

Java 8 first introduced this @FunctionalInterface annotation. This annotation indicates that the type declaration on which it is used is a functional interface. A functional interface can have only one abstract method.

Example 5: @FunctionalInterface annotation example

If we add another abstract method, let's say

Now, when we run the program, we will get the following warning:

It is not mandatory to use @FunctionalInterface annotation. The compiler will consider any interface that meets the functional interface definition as a functional interface.

We use this annotation to make sure that the functional interface has only one abstract method.

However, it can have any number of default and static methods because they have an implementation.

It is also possible to create our own custom annotations.

Here is what you need to know about custom annotation:

Example 6: Custom annotation example

Meta-annotations are annotations that are applied to other annotations.

1. @Retention

The @Retention annotation specifies the level up to which the annotation will be available.

There are 3 types of retention policies:

2. @Documented

By default, custom annotations are not included in the official Java documentation . To include our annotation in the Javadoc documentation, we use the @Documented annotation.

We can restrict an annotation to be applied to specific targets using the @Target annotation.

The ElementType can have one of the following types:

In this example, we have restricted the use of this annotation to methods only.

Note: If the target type is not defined, the annotation can be used for any element.

4. @Inherited

By default, an annotation type cannot be inherited from a superclass. However, if we need to inherit an annotation from a superclass to a subclass, we use the @Inherited annotation.

5. @Repeatable

An annotation that has been marked by @Repeatable can be applied multiple times to the same declaration.

The value defined in the @Repeatable annotation is the container annotation. The container annotation has a variable value of array type of the above repeatable annotation. Here, Universities are the containing annotation type.

Now, the @University annotation can be used multiple times on the same declaration.

If we need to retrieve the annotation data, we can use the Reflection API .

To retrieve annotation values, we use getAnnotationsByType() or getAnnotations() method defined in the Reflection API.

Table of Contents

Sorry about that.

Related Tutorials

Java Tutorial

Try PRO for FREE

Related Articles

Annotations in Java

Annotations are used to provide supplemental information about a program. 

Hierarchy of Annotations in Java  


Note: This program throws compiler error because we have mentioned override, but not overridden, we have overloaded display.


If we remove parameter (int x) or we remove @override, the program compiles fine. 

Categories of Annotations

There are broadly 5 categories of annotations as listed:

Let us discuss and we will be appending code wherever required if so. 

Category 1: Marker Annotations

The only purpose is to mark a declaration. These annotations contain no members and do not consist of any data. Thus, its presence as an annotation is sufficient. Since the marker interface contains no members, simply determining whether it is present or absent is sufficient. @Override is an example of Marker Annotation. 

Category 2: Single value Annotations 

These annotations contain only one member and allow a shorthand form of specifying the value of the member. We only need to specify the value for that member when the annotation is applied and don’t need to specify the name of the member. However, in order to use this shorthand, the name of the member must be a value. 

Category 3: Full Annotations 

These annotations consist of multiple data members, names, values, pairs. 

Category 4: Type Annotations 

These annotations can be applied to any place where a type is being used. For example, we can annotate the return type of a method. These are declared annotated with @Target annotation .

Category 5: Repeating Annotations 

These are the annotations that can be applied to a single item more than once. For an annotation to be repeatable it must be annotated with the @Repeatable annotation, which is defined in the java.lang.annotation package. Its value field specifies the container type for the repeatable annotation. The container is specified as an annotation whose value field is an array of the repeatable annotation type. Hence, to create a repeatable annotation, firstly the container annotation is created, and then the annotation type is specified as an argument to the @Repeatable annotation.

  Predefined/ Standard Annotations

Java popularly defines seven built-in annotations as we have seen up in the hierarchy diagram.

Annotation 1: @Deprecated 

Annotation 2: @Override

It is a marker annotation that can be used only on methods. A method annotated with @Override must override a method from a superclass. If it doesn’t, a compile-time error will result (see this for example). It is used to ensure that a superclass method is actually overridden, and not simply overloaded.

Annotation 3: @SuppressWarnings 

It is used to inform the compiler to suppress specified compiler warnings. The warnings to suppress are specified by name, in string form. This type of annotation can be applied to any type of declaration.

Java groups warnings under two categories. They are deprecated and unchecked . Any unchecked warning is generated when a legacy code interfaces with a code that uses generics.

Annotation 4: @Documented 

It is a marker interface that tells a tool that an annotation is to be documented. Annotations are not included in ‘Javadoc’ comments. The use of @Documented annotation in the code enables tools like Javadoc to process it and include the annotation type information in the generated document.

Annotation 5: @Target 

It is designed to be used only as an annotation to another annotation. @Target takes one argument, which must be constant from the ElementType enumeration. This argument specifies the type of declarations to which the annotation can be applied. The constants are shown below along with the type of the declaration to which they correspond.

We can specify one or more of these values in a @Target annotation. To specify multiple values, we must specify them within a braces-delimited list. For example, to specify that an annotation applies only to fields and local variables, you can use this @Target annotation: @Target({ElementType.FIELD, ElementType.LOCAL_VARIABLE}) @Retention Annotation It determines where and how long the annotation is retent. The 3 values that the @Retention annotation can have:

Annotation 6: @Inherited 

@Inherited is a marker annotation that can be used only on annotation declaration. It affects only annotations that will be used on class declarations. @Inherited causes the annotation for a superclass to be inherited by a subclass. Therefore, when a request for a specific annotation is made to the subclass, if that annotation is not present in the subclass, then its superclass is checked. If that annotation is present in the superclass, and if it is annotated with @Inherited, then that annotation will be returned. 

Annotation 7: User-defined (Custom) 

User-defined annotations can be used to annotate program elements, i.e. variables, constructors, methods, etc. These annotations can be applied just before the declaration of an element (constructor, method, classes, etc). 

Syntax: Declaration

Do keep these certain points as rules for custom annotations before implementing user-defined annotations. 

This article is contributed by Rahul Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to [email protected] See your article appearing on the GeeksforGeeks main page and help other Geeks.  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Please Login to comment...

Improve your Coding Skills with Practice

Start your coding journey now.

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases. See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.

Annotations Basics

The format of an annotation.

In its simplest form, an annotation looks like the following:

The at sign character ( @ ) indicates to the compiler that what follows is an annotation. In the following example, the annotation's name is Override :

The annotation can include elements , which can be named or unnamed, and there are values for those elements:

If there is just one element named value , then the name can be omitted, as in:

If the annotation has no elements, then the parentheses can be omitted, as shown in the previous @Override example.

It is also possible to use multiple annotations on the same declaration:

If the annotations have the same type, then this is called a repeating annotation:

Repeating annotations are supported as of the Java SE 8 release. For more information, see Repeating Annotations .

The annotation type can be one of the types that are defined in the java.lang or java.lang.annotation packages of the Java SE API. In the previous examples, Override and SuppressWarnings are predefined Java annotations . It is also possible to define your own annotation type. The Author and Ebook annotations in the previous example are custom annotation types.

Where Annotations Can Be Used

Annotations can be applied to declarations: declarations of classes, fields, methods, and other program elements. When used on a declaration, each annotation often appears, by convention, on its own line.

As of the Java SE 8 release, annotations can also be applied to the use of types. Here are some examples:

This form of annotation is called a type annotation . For more information, see Type Annotations and Pluggable Type Systems .

About Oracle | Contact Us | Legal Notices | Terms of Use | Your Privacy Rights

Copyright © 1995, 2022 Oracle and/or its affiliates. All rights reserved.

Kotlin Help


Annotations are means of attaching metadata to code. To declare an annotation, put the annotation modifier in front of a class:

Additional attributes of the annotation can be specified by annotating the annotation class with meta-annotations:

@Target specifies the possible kinds of elements which can be annotated with the annotation (such as classes, functions, properties, and expressions);

@Retention specifies whether the annotation is stored in the compiled class files and whether it's visible through reflection at runtime (by default, both are true);

@Repeatable allows using the same annotation on a single element multiple times;

@MustBeDocumented specifies that the annotation is part of the public API and should be included in the class or method signature shown in the generated API documentation.

If you need to annotate the primary constructor of a class, you need to add the constructor keyword to the constructor declaration, and add the annotations before it:

You can also annotate property accessors:


Annotations can have constructors that take parameters.

Allowed parameter types are:

Types that correspond to Java primitive types (Int, Long etc.)

Classes ( Foo::class )

Other annotations

Arrays of the types listed above

Annotation parameters cannot have nullable types, because the JVM does not support storing null as a value of an annotation attribute.

If an annotation is used as a parameter of another annotation, its name is not prefixed with the @ character:

If you need to specify a class as an argument of an annotation, use a Kotlin class ( KClass ). The Kotlin compiler will automatically convert it to a Java class, so that the Java code can access the annotations and arguments normally.


In Java, an annotation type is a form of an interface, so you can implement it and use an instance. As an alternative to this mechanism, Kotlin lets you call a constructor of an annotation class in arbitrary code and similarly use the resulting instance.

Learn more about instantiation of annotation classes in this KEEP .

Annotations can also be used on lambdas. They will be applied to the invoke() method into which the body of the lambda is generated. This is useful for frameworks like Quasar , which uses annotations for concurrency control.

Annotation use-site targets

When you're annotating a property or a primary constructor parameter, there are multiple Java elements which are generated from the corresponding Kotlin element, and therefore multiple possible locations for the annotation in the generated Java bytecode. To specify how exactly the annotation should be generated, use the following syntax:

The same syntax can be used to annotate the entire file. To do this, put an annotation with the target file at the top level of a file, before the package directive or before all imports if the file is in the default package:

If you have multiple annotations with the same target, you can avoid repeating the target by adding brackets after the target and putting all the annotations inside the brackets:

The full list of supported use-site targets is:

property (annotations with this target are not visible to Java)

get (property getter)

set (property setter)

receiver (receiver parameter of an extension function or property)

param (constructor parameter)

setparam (property setter parameter)

delegate (the field storing the delegate instance for a delegated property)

To annotate the receiver parameter of an extension function, use the following syntax:

If you don't specify a use-site target, the target is chosen according to the @Target annotation of the annotation being used. If there are multiple applicable targets, the first applicable target from the following list is used:

Java annotations

Java annotations are 100% compatible with Kotlin:

Since the order of parameters for an annotation written in Java is not defined, you can't use a regular function call syntax for passing the arguments. Instead, you need to use the named argument syntax:

Just like in Java, a special case is the value parameter; its value can be specified without an explicit name:

Arrays as annotation parameters

If the value argument in Java has an array type, it becomes a vararg parameter in Kotlin:

For other arguments that have an array type, you need to use the array literal syntax or arrayOf(...) :

Accessing properties of an annotation instance

Values of an annotation instance are exposed as properties to Kotlin code:

Ability to not generate JVM 1.8+ annotation targets

If a Kotlin annotation has TYPE among its Kotlin targets, the annotation maps to java.lang.annotation.ElementType.TYPE_USE in its list of Java annotation targets. This is just like how the TYPE_PARAMETER Kotlin target maps to the java.lang.annotation.ElementType.TYPE_PARAMETER Java target. This is an issue for Android clients with API levels less than 26, which don't have these targets in the API.

To avoid generating the TYPE_USE and TYPE_PARAMETER annotation targets, use the new compiler argument -Xno-new-java-annotation-targets .

Repeatable annotations

Just like in Java , Kotlin has repeatable annotations, which can be applied to a single code element multiple times. To make your annotation repeatable, mark its declaration with the @kotlin.annotation.Repeatable meta-annotation. This will make it repeatable both in Kotlin and Java. Java repeatable annotations are also supported from the Kotlin side.

The main difference with the scheme used in Java is the absence of a containing annotation , which the Kotlin compiler generates automatically with a predefined name. For an annotation in the example below, it will generate the containing annotation @Tag.Container :

You can set a custom name for a containing annotation by applying the @kotlin.jvm.JvmRepeatable meta-annotation and passing an explicitly declared containing annotation class as an argument:

To extract Kotlin or Java repeatable annotations via reflection, use the KAnnotatedElement.findAnnotations() function.

Learn more about Kotlin repeatable annotations in this KEEP .

List Of All Annotation (Metadata) Programs:


About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected] .

Most Visited Pages

Other Interesting Sites

Javatpoint Logo

Java New Features

Java 9 features, java 8 features, java 7 features, java 4/5 features.


Example to specify annoation for a class

Example to specify annotation for a class, methods or fields.

@Retention annotation is used to specify to what level annotation will be available.

Example to specify the RetentionPolicy

Example of custom annotation: creating, applying and accessing annotation.

Let's see the simple example of creating, applying and accessing annotation.

File: Test.java

How built-in annotaions are used in real scenario?

In real scenario, java programmer only need to apply annotation. He/She doesn't need to create and access annotation. Creating and Accessing annotation is performed by the implementation provider. On behalf of the annotation, java compiler or JVM performs some additional operations.

By default, annotations are not inherited to subclasses. The @Inherited annotation marks the annotation to be inherited to subclasses.


The @Documented Marks the annotation for inclusion in the documentation.


Help Others, Please Share


Learn Latest Tutorials

Splunk tutorial


Tumblr tutorial

Reinforcement Learning

R Programming tutorial

R Programming

RxJS tutorial

React Native

Python Design Patterns

Python Design Patterns

Python Pillow tutorial

Python Pillow

Python Turtle tutorial

Python Turtle

Keras tutorial



Verbal Ability

Interview Questions

Interview Questions

Company Interview Questions

Company Questions

Trending Technologies

Artificial Intelligence

Artificial Intelligence

AWS Tutorial

Cloud Computing

Hadoop tutorial

Data Science

Angular 7 Tutorial

Machine Learning

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures

DAA tutorial

Operating System

Computer Network tutorial

Computer Network

Compiler Design tutorial

Compiler Design

Computer Organization and Architecture

Computer Organization

Discrete Mathematics Tutorial

Discrete Mathematics

Ethical Hacking

Ethical Hacking

Computer Graphics Tutorial

Computer Graphics

Software Engineering

Software Engineering

html tutorial

Web Technology

Cyber Security tutorial

Cyber Security

Automata Tutorial

C Programming

C++ tutorial

Control System

Data Mining Tutorial

Data Mining

Data Warehouse Tutorial

Data Warehouse

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on [email protected] , to get more information about given services.

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected] Duration: 1 week to 2 week

RSS Feed


  1. Java List vs Array List

    annotate list java

  2. Java List Tutorial

    annotate list java

  3. Annotations in Java

    annotate list java

  4. How To Iterate ArrayList In Java

    annotate list java

  5. Java List Collection Tutorial and Examples

    annotate list java

  6. Java List Interface

    annotate list java


  1. How to Create a Custom Annotation in Java || Creating a Custom Annotation

  2. java Programming Insert data and display delete show add

  3. Adobe Premiere Interface

  4. vivo X Fold VS Huawei Mate X2

  5. What is annotation in java

  6. Entwurfsmuster Iterator


  1. Lists with Java Annotations

    In the initialize method you can get the values, which are defined in the annotation. The isValid method is used to validate the object (

  2. Java Annotations Types

    Meta Annotations · 1. @Retention · 2. @Documented · 3. @Target · 4. @Inherited · 5. @Repeatable.

  3. Overview of Java Built-in Annotations

    Overview of Java Built-in Annotations · @FunctionalInterface public interface Adder { int add(int a, int b); } · Adder adder = (a,b) -> a + b; int

  4. Annotations in Java

    Predefined Annotations in java · 1) @Override · 2) @Deprecated · 3) @SuppressWarnings · 4) @Documented Annotations in Java · 5) @Target · 6) @Inherited.

  5. Annotations in Java

    Predefined/ Standard Annotations · Four are imported from java.lang.annotation: @Retention, @Documented, @Target, and @Inherited. · Three are

  6. Predefined Annotation Types

    Predefined Annotation Types ; @Deprecated @Deprecated ; @Override @Override ; @SuppressWarnings @SuppressWarnings ; @SafeVarargs @SafeVarargs ; @FunctionalInterface

  7. Annotations Basics

    lang or java.lang.annotation packages of the Java SE API. In the previous examples, Override and SuppressWarnings are predefined Java annotations. It is

  8. Annotations

    If a Kotlin annotation has TYPE among its Kotlin targets, the annotation maps to java.lang.annotation.ElementType.TYPE_USE in its list of Java

  9. Java Annotations (Metadata) Examples

    Annotations allows us to add some form of metadata information into our source code, but it doesnot change the execution flow of the program. These annotations

  10. Java Annotations

    Java Annotations · class Animal{; void eatSomething(){System.out.println("eating something");}; } · import java.util.*; · class A{; void m(){System.out. · @interface

  11. Using Type Annotations to Improve Your Code

    Java 8 adds type annotations. Annotations on all occurrences of types: @Untainted String query;. List<@NonNull String> strings;.