Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Field

public final class Field
extends AccessibleObject implements Member

java.lang.Object
   ↳ java.lang.reflect.AccessibleObject
     ↳ java.lang.reflect.Field


This class represents a field. Information about the field can be accessed, and the field's value can be accessed dynamically.

Summary

Inherited constants

From interface java.lang.reflect.Member

Public methods

boolean equals(Object other)

Returns true if other has the same declaring class, name and type as this field.

Object get(Object object)

Returns the value of the field in the specified object.

<A extends Annotation> A getAnnotation(Class<A> annotationType)

Returns, for this element, the annotation with the specified type, or null if no annotation with the specified type is present (including inherited annotations).

boolean getBoolean(Object object)

Returns the value of the field in the specified object as a boolean.

byte getByte(Object object)

Returns the value of the field in the specified object as a byte.

char getChar(Object object)

Returns the value of the field in the specified object as a char.

Annotation[] getDeclaredAnnotations()

Returns, for this element, all annotations that are explicitly declared (not inherited).

Class<?> getDeclaringClass()

Returns the class that declares this member.

double getDouble(Object object)

Returns the value of the field in the specified object as a double.

float getFloat(Object object)

Returns the value of the field in the specified object as a float .

Type getGenericType()

Returns the generic type of this field.

int getInt(Object object)

Returns the value of the field in the specified object as an int.

long getLong(Object object)

Returns the value of the field in the specified object as a long.

int getModifiers()

Returns the modifiers for this field.

String getName()

Returns the name of this field.

short getShort(Object object)

Returns the value of the field in the specified object as a short .

Class<?> getType()

Return the Class associated with the type of this field.

int hashCode()

Returns an integer hash code for this object.

Equivalent to getDeclaringClass().getName().hashCode() ^ getName().hashCode().

boolean isAnnotationPresent(Class<? extends Annotation> annotationType)

Indicates whether or not this element has an annotation with the specified annotation type (including inherited annotations).

boolean isEnumConstant()

Indicates whether or not this field is an enumeration constant.

boolean isSynthetic()

Indicates whether or not this field is synthetic.

void set(Object object, Object value)

Sets the value of the field in the specified object to the value.

void setBoolean(Object object, boolean value)

Sets the value of the field in the specified object to the boolean value.

void setByte(Object object, byte value)

Sets the value of the field in the specified object to the byte value.

void setChar(Object object, char value)

Sets the value of the field in the specified object to the char value.

void setDouble(Object object, double value)

Sets the value of the field in the specified object to the double value.

void setFloat(Object object, float value)

Sets the value of the field in the specified object to the float value.

void setInt(Object object, int value)

Set the value of the field in the specified object to the int value.

void setLong(Object object, long value)

Sets the value of the field in the specified object to the long value.

void setShort(Object object, short value)

Sets the value of the field in the specified object to the short value.

String toGenericString()

Returns the string representation of this field, including the field's generic type.

String toString()

Returns a string containing a concise, human-readable description of this field.

Inherited methods

From class java.lang.reflect.AccessibleObject
From class java.lang.Object
From interface java.lang.reflect.AnnotatedElement
From interface java.lang.reflect.Member

Public methods

equals

Added in API level 1
boolean equals (Object other)

Returns true if other has the same declaring class, name and type as this field.

Parameters
other Object: the object to compare this instance with.
Returns
boolean true if the specified object is equal to this Object; false otherwise.

get

Added in API level 1
Object get (Object object)

Returns the value of the field in the specified object. This reproduces the effect of object.fieldName

If the type of this field is a primitive type, the field value is automatically boxed.

If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

Parameters
object Object: the object to access
Returns
Object the field value, possibly boxed
Throws
NullPointerException if the object is null and the field is non-static
IllegalArgumentException if the object is not compatible with the declaring class
IllegalAccessException if this field is not accessible

getAnnotation

Added in API level 14
A getAnnotation (Class<A> annotationType)

Returns, for this element, the annotation with the specified type, or null if no annotation with the specified type is present (including inherited annotations).

Parameters
annotationType Class: the type of the annotation to search for
Returns
A the annotation with the specified type or null

getBoolean

Added in API level 1
boolean getBoolean (Object object)

Returns the value of the field in the specified object as a boolean. This reproduces the effect of object.fieldName

If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

Parameters
object Object: the object to access
Returns
boolean the field value
Throws
NullPointerException if the object is null and the field is non-static
IllegalArgumentException if the object is not compatible with the declaring class
IllegalAccessException if this field is not accessible

getByte

Added in API level 1
byte getByte (Object object)

Returns the value of the field in the specified object as a byte. This reproduces the effect of object.fieldName

If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

Parameters
object Object: the object to access
Returns
byte the field value
Throws
NullPointerException if the object is null and the field is non-static
IllegalArgumentException if the object is not compatible with the declaring class
IllegalAccessException if this field is not accessible

getChar

Added in API level 1
char getChar (Object object)

Returns the value of the field in the specified object as a char. This reproduces the effect of object.fieldName

If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

Parameters
object Object: the object to access
Returns
char the field value
Throws
NullPointerException if the object is null and the field is non-static
IllegalArgumentException if the object is not compatible with the declaring class
IllegalAccessException if this field is not accessible

getDeclaredAnnotations

Added in API level 1
Annotation[] getDeclaredAnnotations ()

Returns, for this element, all annotations that are explicitly declared (not inherited). If there are no declared annotations present, this method returns a zero length array.

Returns
Annotation[] an array of annotations declared for this element

getDeclaringClass

Added in API level 1
Class<?> getDeclaringClass ()

Returns the class that declares this member.

Returns
Class<?> the declaring class

getDouble

Added in API level 1
double getDouble (Object object)

Returns the value of the field in the specified object as a double. This reproduces the effect of object.fieldName

If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

Parameters
object Object: the object to access
Returns
double the field value
Throws
NullPointerException if the object is null and the field is non-static
IllegalArgumentException if the object is not compatible with the declaring class
IllegalAccessException if this field is not accessible

getFloat

Added in API level 1
float getFloat (Object object)

Returns the value of the field in the specified object as a float . This reproduces the effect of object.fieldName

If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

Parameters
object Object: the object to access
Returns
float the field value
Throws
NullPointerException if the object is null and the field is non-static
IllegalArgumentException if the object is not compatible with the declaring class
IllegalAccessException if this field is not accessible

getGenericType

Added in API level 1
Type getGenericType ()

Returns the generic type of this field.

Returns
Type the generic type
Throws
GenericSignatureFormatError if the generic field signature is invalid
TypeNotPresentException if the generic type points to a missing type
MalformedParameterizedTypeException if the generic type points to a type that cannot be instantiated for some reason

getInt

Added in API level 1
int getInt (Object object)

Returns the value of the field in the specified object as an int. This reproduces the effect of object.fieldName

If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

Parameters
object Object: the object to access
Returns
int the field value
Throws
NullPointerException if the object is null and the field is non-static
IllegalArgumentException if the object is not compatible with the declaring class
IllegalAccessException if this field is not accessible

getLong

Added in API level 1
long getLong (Object object)

Returns the value of the field in the specified object as a long. This reproduces the effect of object.fieldName

If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

Parameters
object Object: the object to access
Returns
long the field value
Throws
NullPointerException if the object is null and the field is non-static
IllegalArgumentException if the object is not compatible with the declaring class
IllegalAccessException if this field is not accessible

getModifiers

Added in API level 1
int getModifiers ()

Returns the modifiers for this field. The Modifier class should be used to decode the result.

Returns
int the modifiers for this field

See also:

getName

Added in API level 1
String getName ()

Returns the name of this field.

Returns
String the name of this field

getShort

Added in API level 1
short getShort (Object object)

Returns the value of the field in the specified object as a short . This reproduces the effect of object.fieldName

If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

Parameters
object Object: the object to access
Returns
short the field value
Throws
NullPointerException if the object is null and the field is non-static
IllegalArgumentException if the object is not compatible with the declaring class
IllegalAccessException if this field is not accessible

getType

Added in API level 1
Class<?> getType ()

Return the Class associated with the type of this field.

Returns
Class<?> the type of this field

hashCode

Added in API level 1
int hashCode ()

Returns an integer hash code for this object. By contract, any two objects for which equals(Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

Note that hash values must not change over time unless information used in equals comparisons also changes.

See Writing a correct hashCode method if you intend implementing your own hashCode method.

Equivalent to getDeclaringClass().getName().hashCode() ^ getName().hashCode().

Returns
int this object's hash code.

isAnnotationPresent

Added in API level 1
boolean isAnnotationPresent (Class<? extends Annotation> annotationType)

Indicates whether or not this element has an annotation with the specified annotation type (including inherited annotations).

Parameters
annotationType Class: the type of the annotation to search for
Returns
boolean true if the annotation exists, false otherwise

isEnumConstant

Added in API level 1
boolean isEnumConstant ()

Indicates whether or not this field is an enumeration constant.

Returns
boolean true if this field is an enumeration constant, false otherwise

isSynthetic

Added in API level 1
boolean isSynthetic ()

Indicates whether or not this field is synthetic.

Returns
boolean true if this field is synthetic, false otherwise

set

Added in API level 1
void set (Object object, 
                Object value)

Sets the value of the field in the specified object to the value. This reproduces the effect of object.fieldName = value

If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

If the field type is a primitive type, the value is automatically unboxed. If the unboxing fails, an IllegalArgumentException is thrown. If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

Parameters
object Object: the object to access
value Object: the new value
Throws
NullPointerException if the object is null and the field is non-static
IllegalArgumentException if the object is not compatible with the declaring class
IllegalAccessException if this field is not accessible

setBoolean

Added in API level 1
void setBoolean (Object object, 
                boolean value)

Sets the value of the field in the specified object to the boolean value. This reproduces the effect of object.fieldName = value

If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

Parameters
object Object: the object to access
value boolean: the new value
Throws
NullPointerException if the object is null and the field is non-static
IllegalArgumentException if the object is not compatible with the declaring class
IllegalAccessException if this field is not accessible

setByte

Added in API level 1
void setByte (Object object, 
                byte value)

Sets the value of the field in the specified object to the byte value. This reproduces the effect of object.fieldName = value

If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

Parameters
object Object: the object to access
value byte: the new value
Throws
NullPointerException if the object is null and the field is non-static
IllegalArgumentException if the object is not compatible with the declaring class
IllegalAccessException if this field is not accessible

setChar

Added in API level 1
void setChar (Object object, 
                char value)

Sets the value of the field in the specified object to the char value. This reproduces the effect of object.fieldName = value

If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

Parameters
object Object: the object to access
value char: the new value
Throws
NullPointerException if the object is null and the field is non-static
IllegalArgumentException if the object is not compatible with the declaring class
IllegalAccessException if this field is not accessible

setDouble

Added in API level 1
void setDouble (Object object, 
                double value)

Sets the value of the field in the specified object to the double value. This reproduces the effect of object.fieldName = value

If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

Parameters
object Object: the object to access
value double: the new value
Throws
NullPointerException if the object is null and the field is non-static
IllegalArgumentException if the object is not compatible with the declaring class
IllegalAccessException if this field is not accessible

setFloat

Added in API level 1
void setFloat (Object object, 
                float value)

Sets the value of the field in the specified object to the float value. This reproduces the effect of object.fieldName = value

If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

Parameters
object Object: the object to access
value float: the new value
Throws
NullPointerException if the object is null and the field is non-static
IllegalArgumentException if the object is not compatible with the declaring class
IllegalAccessException if this field is not accessible

setInt

Added in API level 1
void setInt (Object object, 
                int value)

Set the value of the field in the specified object to the int value. This reproduces the effect of object.fieldName = value

If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

Parameters
object Object: the object to access
value int: the new value
Throws
NullPointerException if the object is null and the field is non-static
IllegalArgumentException if the object is not compatible with the declaring class
IllegalAccessException if this field is not accessible

setLong

Added in API level 1
void setLong (Object object, 
                long value)

Sets the value of the field in the specified object to the long value. This reproduces the effect of object.fieldName = value

If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

Parameters
object Object: the object to access
value long: the new value
Throws
NullPointerException if the object is null and the field is non-static
IllegalArgumentException if the object is not compatible with the declaring class
IllegalAccessException if this field is not accessible

setShort

Added in API level 1
void setShort (Object object, 
                short value)

Sets the value of the field in the specified object to the short value. This reproduces the effect of object.fieldName = value

If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.

If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.

Parameters
object Object: the object to access
value short: the new value
Throws
NullPointerException if the object is null and the field is non-static
IllegalArgumentException if the object is not compatible with the declaring class
IllegalAccessException if this field is not accessible

toGenericString

Added in API level 1
String toGenericString ()

Returns the string representation of this field, including the field's generic type.

Returns
String the string representation of this field

toString

Added in API level 1
String toString ()

Returns a string containing a concise, human-readable description of this field.

The format of the string is:

  1. modifiers (if any)
  2. type
  3. declaring class name
  4. '.'
  5. field name

For example: public static java.io.InputStream java.lang.System.in

Returns
String a printable representation for this field
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.