Class Field
- All Implemented Interfaces:
AnnotatedElement,Member
public final class Field extends AccessibleObject implements Member
-
Field Summary
Fields Modifier and Type Field Description static Comparator<Field>ORDER_BY_NAME_AND_DECLARING_CLASSOrders fields by their name and declaring class. -
Method Summary
Modifier and Type Method Description booleanequals(Object object)Indicates whether or not the specifiedobjectis equal to this field.Objectget(Object object)Returns the value of the field in the specified object.booleangetBoolean(Object object)Returns the value of the field in the specified object as aboolean.bytegetByte(Object object)Returns the value of the field in the specified object as abyte.chargetChar(Object object)Returns the value of the field in the specified object as achar.protected Annotation[]getDeclaredAnnotations(boolean copy)Class<?>getDeclaringClass()Returns the class that declares this field.doublegetDouble(Object object)Returns the value of the field in the specified object as adouble.floatgetFloat(Object object)Returns the value of the field in the specified object as afloat.TypegetGenericType()Returns the generic type of this field.intgetInt(Object object)Returns the value of the field in the specified object as anint.longgetLong(Object object)Returns the value of the field in the specified object as along.intgetModifiers()Returns the modifiers for this field.StringgetName()Returns the name of this field.shortgetShort(Object object)Returns the value of the field in the specified object as ashort.protected StringgetSignatureAttribute()Class<?>getType()Return theClassassociated with the type of this field.inthashCode()Returns an integer hash code for this field.booleanisEnumConstant()Indicates whether or not this field is an enumeration constant.booleanisSynthetic()Indicates whether or not this field is synthetic.voidset(Object object, Object value)Sets the value of the field in the specified object to the value.voidsetBoolean(Object object, boolean value)Sets the value of the field in the specified object to thebooleanvalue.voidsetByte(Object object, byte value)Sets the value of the field in the specified object to thebytevalue.voidsetChar(Object object, char value)Sets the value of the field in the specified object to thecharvalue.voidsetDouble(Object object, double value)Sets the value of the field in the specified object to thedoublevalue.voidsetFloat(Object object, float value)Sets the value of the field in the specified object to thefloatvalue.voidsetInt(Object object, int value)Set the value of the field in the specified object to theintvalue.voidsetLong(Object object, long value)Sets the value of the field in the specified object to thelongvalue.voidsetShort(Object object, short value)Sets the value of the field in the specified object to theshortvalue.StringtoGenericString()Returns the string representation of this field, including the field's generic type.StringtoString()Returns a string containing a concise, human-readable description of this field.Methods inherited from class java.lang.reflect.AccessibleObject
getAnnotation, getAnnotations, getDeclaredAnnotations, isAccessible, isAnnotationPresent, setAccessible, setAccessible
-
Field Details
-
ORDER_BY_NAME_AND_DECLARING_CLASS
Orders fields by their name and declaring class.
-
-
Method Details
-
getSignatureAttribute
-
isSynthetic
public boolean isSynthetic()Indicates whether or not this field is synthetic.- Specified by:
isSyntheticin interfaceMember- Returns:
trueif this field is synthetic,falseotherwise
-
toGenericString
Returns the string representation of this field, including the field's generic type.- Returns:
- the string representation of this field
-
isEnumConstant
public boolean isEnumConstant()Indicates whether or not this field is an enumeration constant.- Returns:
trueif this field is an enumeration constant,falseotherwise
-
getGenericType
Returns the generic type of this field.- Returns:
- the generic type
- Throws:
GenericSignatureFormatError- if the generic field signature is invalidTypeNotPresentException- if the generic type points to a missing typeMalformedParameterizedTypeException- if the generic type points to a type that cannot be instantiated for some reason
-
getDeclaredAnnotations
- Overrides:
getDeclaredAnnotationsin classAccessibleObject
-
equals
Indicates whether or not the specifiedobjectis equal to this field. To be equal, the specified object must be an instance ofFieldwith the same declaring class, type and name as this field.- Overrides:
equalsin classObject- Parameters:
object- the object to compare- Returns:
trueif the specified object is equal to this method,falseotherwise- See Also:
hashCode()
-
get
Returns the value of the field in the specified object. This reproduces the effect ofobject.fieldNameIf 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- the object to access- Returns:
- the field value, possibly boxed
- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
getBoolean
Returns the value of the field in the specified object as aboolean. This reproduces the effect ofobject.fieldNameIf 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- the object to access- Returns:
- the field value
- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
getByte
Returns the value of the field in the specified object as abyte. This reproduces the effect ofobject.fieldNameIf 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- the object to access- Returns:
- the field value
- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
getChar
Returns the value of the field in the specified object as achar. This reproduces the effect ofobject.fieldNameIf 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- the object to access- Returns:
- the field value
- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
getDeclaringClass
Returns the class that declares this field.- Specified by:
getDeclaringClassin interfaceMember- Returns:
- the declaring class
-
getDouble
Returns the value of the field in the specified object as adouble. This reproduces the effect ofobject.fieldNameIf 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- the object to access- Returns:
- the field value
- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
getFloat
Returns the value of the field in the specified object as afloat. This reproduces the effect ofobject.fieldNameIf 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- the object to access- Returns:
- the field value
- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
getInt
Returns the value of the field in the specified object as anint. This reproduces the effect ofobject.fieldNameIf 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- the object to access- Returns:
- the field value
- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
getLong
Returns the value of the field in the specified object as along. This reproduces the effect ofobject.fieldNameIf 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- the object to access- Returns:
- the field value
- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
getModifiers
public int getModifiers()Returns the modifiers for this field. TheModifierclass should be used to decode the result.- Specified by:
getModifiersin interfaceMember- Returns:
- the modifiers for this field
- See Also:
Modifier
-
getName
Returns the name of this field. -
getShort
Returns the value of the field in the specified object as ashort. This reproduces the effect ofobject.fieldNameIf 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- the object to access- Returns:
- the field value
- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
getType
Return theClassassociated with the type of this field.- Returns:
- the type of this field
-
hashCode
public int hashCode()Returns an integer hash code for this field. Objects which are equal return the same value for this method.The hash code for a Field is the exclusive-or combination of the hash code of the field's name and the hash code of the name of its declaring class.
- Overrides:
hashCodein classObject- Returns:
- the hash code for this field
- See Also:
equals(java.lang.Object)
-
set
public void set(Object object, Object value) throws IllegalAccessException, IllegalArgumentExceptionSets the value of the field in the specified object to the value. This reproduces the effect ofobject.fieldName = valueIf 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- the object to accessvalue- the new value- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
setBoolean
public void setBoolean(Object object, boolean value) throws IllegalAccessException, IllegalArgumentExceptionSets the value of the field in the specified object to thebooleanvalue. This reproduces the effect ofobject.fieldName = valueIf 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- the object to accessvalue- the new value- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
setByte
public void setByte(Object object, byte value) throws IllegalAccessException, IllegalArgumentExceptionSets the value of the field in the specified object to thebytevalue. This reproduces the effect ofobject.fieldName = valueIf 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- the object to accessvalue- the new value- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
setChar
public void setChar(Object object, char value) throws IllegalAccessException, IllegalArgumentExceptionSets the value of the field in the specified object to thecharvalue. This reproduces the effect ofobject.fieldName = valueIf 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- the object to accessvalue- the new value- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
setDouble
public void setDouble(Object object, double value) throws IllegalAccessException, IllegalArgumentExceptionSets the value of the field in the specified object to thedoublevalue. This reproduces the effect ofobject.fieldName = valueIf 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- the object to accessvalue- the new value- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
setFloat
public void setFloat(Object object, float value) throws IllegalAccessException, IllegalArgumentExceptionSets the value of the field in the specified object to thefloatvalue. This reproduces the effect ofobject.fieldName = valueIf 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- the object to accessvalue- the new value- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
setInt
public void setInt(Object object, int value) throws IllegalAccessException, IllegalArgumentExceptionSet the value of the field in the specified object to theintvalue. This reproduces the effect ofobject.fieldName = valueIf 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- the object to accessvalue- the new value- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
setLong
public void setLong(Object object, long value) throws IllegalAccessException, IllegalArgumentExceptionSets the value of the field in the specified object to thelongvalue. This reproduces the effect ofobject.fieldName = valueIf 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- the object to accessvalue- the new value- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
setShort
public void setShort(Object object, short value) throws IllegalAccessException, IllegalArgumentExceptionSets the value of the field in the specified object to theshortvalue. This reproduces the effect ofobject.fieldName = valueIf 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- the object to accessvalue- the new value- Throws:
NullPointerException- if the object isnulland the field is non-staticIllegalArgumentException- if the object is not compatible with the declaring classIllegalAccessException- if this field is not accessible
-
toString
Returns a string containing a concise, human-readable description of this field.The format of the string is:
- modifiers (if any)
- type
- declaring class name
- '.'
- field name
For example:
public static java.io.InputStream java.lang.System.in
-