Stats

851 Contributors: 29 Tuesday, August 1, 2017
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

Annotations

Introduction

In Java, an annotation is a form of syntactic metadata that can be added to Java source code. It provides data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate. Classes, methods, variables, parameters and packages are allowed to be annotated.

Syntax

  • @AnnotationName // 'Marker annotation' (no parameters)
  • @AnnotationName(someValue) // sets parameter with the name 'value'
  • @AnnotationName(param1 = value1) // named parameter
  • @AnnotationName(param1 = value1, param2 = value2) // multiple named parameters
  • @AnnotationName(param1 = {1, 2, 3}) // named array parameter
  • @AnnotationName({value1}) // array with single element as parameter with the name 'value'

Remarks

Parameter types

Only constant expressions of following types are allowed for parameters, as well as arrays of these types:

  • String
  • Class
  • primitive types
  • Enum types
  • Annotation Types

Related Examples