# Interface VariableElement

All Superinterfaces:
  `AnnotatedConstruct, Element`

public interface VariableElement extends Element
Represents a field, `enum` constant, method or constructor parameter, local variable, resource variable, or exception parameter.

Since:
  1.6

- ## Method Summary

  Modifier and Type
  Method
  Description
  `TypeMirror`
  `asType()`
  Returns the type of this variable.
  `Object`
  `getConstantValue()`
  Returns the value of this variable if this is a `final` field initialized to a compile-time constant.
  `Element`
  `getEnclosingElement()`
  Returns the enclosing element of this variable.
  `Name`
  `getSimpleName()`
  Returns the simple name of this variable element.
  `default boolean`
  `isUnnamed()`
  Returns `true` if this is an unnamed variable and `false` otherwise.

  ### Methods declared in interface Element

  `accept, equals, getAnnotation, getAnnotationMirrors, getAnnotationsByType, getEnclosedElements, getKind, getModifiers, hashCode`
  Modifier and Type
  Method
  Description
  `<R,P> R`
  `accept(ElementVisitor<R,P> v, P p)`
  Applies a visitor to this element.
  `boolean`
  `equals(Object obj)`
  Returns `true` if the argument represents the same element as `this`, or `false` otherwise.
  `<A extends Annotation>
  A`
  `getAnnotation(Class<A> annotationType)`
  Returns this construct's annotation of the specified type if such an annotation is present, else `null`.
  `List<? extends AnnotationMirror>`
  `getAnnotationMirrors()`
  Returns the annotations that are directly present on this construct.
  `<A extends Annotation>
  A[]`
  `getAnnotationsByType(Class<A> annotationType)`
  Returns annotations of the specified type that are associated with this construct.
  `List<? extends Element>`
  `getEnclosedElements()`
  Returns the elements that are, loosely speaking, directly enclosed by this element.
  `ElementKind`
  `getKind()`
  Returns the `kind` of this element.
  `Set<Modifier>`
  `getModifiers()`
  Returns the modifiers of this element, excluding annotations.
  `int`
  `hashCode()`
  Obeys the general contract of `Object.hashCode`.

- ## Method Details

  - ### asType

    TypeMirror asType()
    Returns the type of this variable. Note that the types of variables range over many kinds of types, including primitive types, declared types, and array types, among others.
    Specified by:
      `asType` in interface `Element`
    Returns:
      the type of this variable
    See Also:
      - `TypeKind`

  - ### getConstantValue

    Object getConstantValue()
    Returns the value of this variable if this is a `final` field initialized to a compile-time constant. Returns `null` otherwise. The value will be of a primitive type or a `String`. If the value is of a primitive type, it is wrapped in the appropriate wrapper class (such as `Integer`).

    Note that not all `final` fields will have constant values. In particular, `enum` constants are not considered to be compile-time constants. To have a constant value, a field's type must be either a primitive type or `String`.

    Returns:
      the value of this variable if this is a `final` field initialized to a compile-time constant, or `null` otherwise
    See Java Language Specification:
      15.29 Constant Expressions
      4.12.4 final Variables
    See Also:
      - `Elements.getConstantExpression(Object)`

  - ### getSimpleName

    Name getSimpleName()
    Returns the simple name of this variable element.

    For method and constructor parameters, the name of each parameter must be distinct from the names of all other parameters of the same executable. If the original source names are not available, an implementation may synthesize names subject to the distinctness requirement above.

    For variables, the name of each variable is returned, or an empty name if the variable is unnamed.

    Specified by:
      `getSimpleName` in interface `Element`
    Returns:
      the simple name of this variable element
    See Also:
      - `PackageElement.getSimpleName()`
      - `ExecutableElement.getSimpleName()`
      - `TypeElement.getSimpleName()`
      - `getSimpleName()`
      - `ModuleElement.getSimpleName()`
      - `RecordComponentElement.getSimpleName()`

  - ### getEnclosingElement

    Element getEnclosingElement()
    Returns the enclosing element of this variable. The enclosing element of a method or constructor parameter is the executable declaring the parameter.
    Specified by:
      `getEnclosingElement` in interface `Element`
    Returns:
      the enclosing element of this variable
    See Also:
      - `Elements.getPackageOf(Element)`

  - ### isUnnamed

    default boolean isUnnamed()
    Returns `true` if this is an unnamed variable and `false` otherwise.
    Implementation Requirements:
      The default implementation of this method calls `getSimpleName()` and returns `true` if the result is empty and `false` otherwise.
    Returns:
      `true` if this is an unnamed variable and `false` otherwise
    See Java Language Specification:
      6.1 Declarations
      14.4 Local Variable Declarations
    Since:
      22