AnnotationElement (Java SE 26 & JDK 26)
Description: declaration: module: java.base, package: java.lang.classfile, interface: AnnotationElement
Keywords: java.lang.classfile.AnnotationElement interface

# Interface AnnotationElement

public sealed interface AnnotationElement
Models an element-value pair in the `element_value_pairs` table in the `annotation` structure defined in JVMS 4.7.16 or the `type_annotation` structure defined in JVMS 4.7.20.

Two `AnnotationElement` objects should be compared using the `equals` method.

See Java Virtual Machine Specification:
  4.7.16.1 The `element_value` structure
Since:
  24
See Also:
  - `Annotation`
  - Annotations in core reflection

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Utf8Entry`
  `name()`
  Returns the element name.
  `static AnnotationElement`
  `of(Utf8Entry name, AnnotationValue value)`
  Returns an element-value pair.
  `static AnnotationElement`
  `of(String name, AnnotationValue value)`
  Returns an element-value pair.
  `static AnnotationElement`
  `ofAnnotation(String name, Annotation value)`
  Returns an element-value pair for an annotation-valued element.
  `static AnnotationElement`
  `ofArray(String name, AnnotationValue... values)`
  Returns an element-value pair for an array-valued element.
  `static AnnotationElement`
  `ofBoolean(String name, boolean value)`
  Returns an element-value pair for a boolean-valued element.
  `static AnnotationElement`
  `ofByte(String name, byte value)`
  Returns an element-value pair for a byte-valued element.
  `static AnnotationElement`
  `ofChar(String name, char value)`
  Returns an element-value pair for a char-valued element.
  `static AnnotationElement`
  `ofClass(String name, ClassDesc value)`
  Returns an element-value pair for a class-valued element.
  `static AnnotationElement`
  `ofDouble(String name, double value)`
  Returns an element-value pair for a double-valued element.
  `static AnnotationElement`
  `ofFloat(String name, float value)`
  Returns an element-value pair for a float-valued element.
  `static AnnotationElement`
  `ofInt(String name, int value)`
  Returns an element-value pair for an int-valued element.
  `static AnnotationElement`
  `ofLong(String name, long value)`
  Returns an element-value pair for a long-valued element.
  `static AnnotationElement`
  `ofShort(String name, short value)`
  Returns an element-value pair for a short-valued element.
  `static AnnotationElement`
  `ofString(String name, String value)`
  Returns an element-value pair for a string-valued element.
  `AnnotationValue`
  `value()`
  Returns the element value.

- ## Method Details

  - ### name

    Utf8Entry name()
    Returns the element name.
    API Note:
      In Java source code, by convention, the name of the sole element in a single-element annotation interface is `value`. (JLS 9.6.1) This is the case for single-element annotations (JLS 9.7.3) and container annotations for multiple annotations (JLS 9.6.3).
    Returns:
      the element name

  - ### value

    AnnotationValue value()
    Returns the element value.
    Returns:
      the element value

  - ### of

    static AnnotationElement of(Utf8Entry name, AnnotationValue value)
    Returns an element-value pair.
    Parameters:
      `name` - the name of the key
      `value` - the associated value
    Returns:
      an element-value pair

  - ### of

    static AnnotationElement of(String name, AnnotationValue value)
    Returns an element-value pair.
    Parameters:
      `name` - the name of the key
      `value` - the associated value
    Returns:
      an element-value pair

  - ### ofClass

    static AnnotationElement ofClass(String name, ClassDesc value)
    Returns an element-value pair for a class-valued element.
    Parameters:
      `name` - the name of the key
      `value` - the associated value
    Returns:
      an element-value pair for a class-valued element
    See Also:
      - `AnnotationValue::ofClass`

  - ### ofString

    static AnnotationElement ofString(String name, String value)
    Returns an element-value pair for a string-valued element.
    Parameters:
      `name` - the name of the key
      `value` - the associated value
    Returns:
      an element-value pair for a string-valued element
    See Also:
      - `AnnotationValue::ofString`

  - ### ofLong

    static AnnotationElement ofLong(String name, long value)
    Returns an element-value pair for a long-valued element.
    Parameters:
      `name` - the name of the key
      `value` - the associated value
    Returns:
      an element-value pair for a long-valued element
    See Also:
      - `AnnotationValue::ofLong`

  - ### ofInt

    static AnnotationElement ofInt(String name, int value)
    Returns an element-value pair for an int-valued element.
    Parameters:
      `name` - the name of the key
      `value` - the associated value
    Returns:
      an element-value pair for an int-valued element
    See Also:
      - `AnnotationValue::ofInt`

  - ### ofChar

    static AnnotationElement ofChar(String name, char value)
    Returns an element-value pair for a char-valued element.
    Parameters:
      `name` - the name of the key
      `value` - the associated value
    Returns:
      an element-value pair for a char-valued element
    See Also:
      - `AnnotationValue::ofChar`

  - ### ofShort

    static AnnotationElement ofShort(String name, short value)
    Returns an element-value pair for a short-valued element.
    Parameters:
      `name` - the name of the key
      `value` - the associated value
    Returns:
      an element-value pair for a short-valued element
    See Also:
      - `AnnotationValue::ofShort`

  - ### ofByte

    static AnnotationElement ofByte(String name, byte value)
    Returns an element-value pair for a byte-valued element.
    Parameters:
      `name` - the name of the key
      `value` - the associated value
    Returns:
      an element-value pair for a byte-valued element
    See Also:
      - `AnnotationValue::ofByte`

  - ### ofBoolean

    static AnnotationElement ofBoolean(String name, boolean value)
    Returns an element-value pair for a boolean-valued element.
    Parameters:
      `name` - the name of the key
      `value` - the associated value
    Returns:
      an element-value pair for a boolean-valued element
    See Also:
      - `AnnotationValue::ofBoolean`

  - ### ofDouble

    static AnnotationElement ofDouble(String name, double value)
    Returns an element-value pair for a double-valued element.
    Parameters:
      `name` - the name of the key
      `value` - the associated value
    Returns:
      an element-value pair for a double-valued element
    See Also:
      - `AnnotationValue::ofDouble`

  - ### ofFloat

    static AnnotationElement ofFloat(String name, float value)
    Returns an element-value pair for a float-valued element.
    Parameters:
      `name` - the name of the key
      `value` - the associated value
    Returns:
      an element-value pair for a float-valued element
    See Also:
      - `AnnotationValue::ofFloat`

  - ### ofAnnotation

    static AnnotationElement ofAnnotation(String name, Annotation value)
    Returns an element-value pair for an annotation-valued element.
    Parameters:
      `name` - the name of the key
      `value` - the associated value
    Returns:
      an element-value pair for an annotation-valued element
    See Also:
      - `AnnotationValue::ofAnnotation`

  - ### ofArray

    static AnnotationElement ofArray(String name, AnnotationValue... values)
    Returns an element-value pair for an array-valued element.
    Parameters:
      `name` - the name of the key
      `values` - the associated values
    Returns:
      an element-value pair for an array-valued element
    Throws:
      `IllegalArgumentException` - if the number of associated values exceeds the limit of `u2`
    See Also:
      - `AnnotationValue::ofArray`