Field
provides information about, and dynamic access to, a
single field of a class or an interface. The reflected field may
be a class (static) field or an instance field.
A Field
permits widening conversions to occur during a get or
set access operation, but throws an IllegalArgumentException
if a
narrowing conversion would occur.
Field
against the specified object. Returns
true if the objects are the same. Two Field
objects are the same if
they were declared by the same class and have the same name
and type.Field
, on
the specified object. The value is automatically wrapped in an
object if it has a primitive type.
The underlying field's value is obtained as follows:
If the underlying field is a static field, the obj
argument
is ignored; it may be null.
Otherwise, the underlying field is an instance field. If the
specified obj
argument is null, the method throws a
NullPointerException.
If the specified object is not an
instance of the class or interface declaring the underlying
field, the method throws an IllegalArgumentException
.
If this Field
object enforces Java language access control, and
the underlying field is inaccessible, the method throws an
IllegalAccessException
.
If the underlying field is static, the class that declared the
field is initialized if it has not already been initialized.
Otherwise, the value is retrieved from the underlying instance or static field. If the field has a primitive type, the value is wrapped in an object before being returned, otherwise it is returned as is.
If the field is hidden in the type of obj
,
the field's value is obtained according to the preceding rules.
boolean
field.byte
field.char
or of another primitive type convertible to
type char
via a widening conversion.double
or of another primitive type convertible to
type double
via a widening conversion.float
or of another primitive type convertible to
type float
via a widening conversion.If the Type is a parameterized type, the Type object returned must accurately reflect the actual type parameters used in the source code.
If an the type of the underlying field is a type variable or a parameterized type, it is created. Otherwise, it is resolved.
int
or of another primitive type convertible to
type int
via a widening conversion.long
or of another primitive type convertible to
type long
via a widening conversion.short
or of another primitive type convertible to
type short
via a widening conversion.Class
object that identifies the
declared type for the field represented by this
Field
object.Field
. This is computed as the
exclusive-or of the hashcodes for the underlying field's
declaring class name and its name.wait
methods.
The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:
synchronized
statement
that synchronizes on the object.
Class,
by executing a
synchronized static method of that class.
Only one thread at a time can own an object's monitor.
wait
methods.
The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object. The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner
of this object's monitor. See the notify
method for a
description of the ways in which a thread can become the owner of
a monitor.
Field
object on the
specified object argument to the specified new value. The new
value is automatically unwrapped if the underlying field has a
primitive type.
The operation proceeds as follows:
If the underlying field is static, the obj
argument is
ignored; it may be null.
Otherwise the underlying field is an instance field. If the
specified object argument is null, the method throws a
NullPointerException
. If the specified object argument is not
an instance of the class or interface declaring the underlying
field, the method throws an IllegalArgumentException
.
If this Field
object enforces Java language access control, and
the underlying field is inaccessible, the method throws an
IllegalAccessException
.
If the underlying field is final, the method throws an
IllegalAccessException
unless
setAccessible(true)
has succeeded for this field
and this field is non-static. Setting a final field in this way
is meaningful only during deserialization or reconstruction of
instances of classes with blank final fields, before they are
made available for access by other parts of a program. Use in
any other context may have unpredictable effects, including cases
in which other parts of a program continue to use the original
value of this field.
If the underlying field is of a primitive type, an unwrapping
conversion is attempted to convert the new value to a value of
a primitive type. If this attempt fails, the method throws an
IllegalArgumentException
.
If, after possible unwrapping, the new value cannot be
converted to the type of the underlying field by an identity or
widening conversion, the method throws an
IllegalArgumentException
.
If the underlying field is static, the class that declared the field is initialized if it has not already been initialized.
The field is set to the possibly unwrapped and widened new value.
If the field is hidden in the type of obj
,
the field's value is set according to the preceding rules.
First, if there is a security manager, its
checkPermission
method is called with a
ReflectPermission("suppressAccessChecks")
permission.
A SecurityException
is raised if flag
is
true
but accessibility of any of the elements of the input
array
may not be changed (for example, if the element
object is a Constructor
object for the class java.lang.Class
). In the event of such a SecurityException, the
accessibility of objects is set to flag
for array elements
upto (and excluding) the element for which the exception occurred; the
accessibility of elements beyond (and including) the element for which
the exception occurred is unchanged.
First, if there is a security manager, its
checkPermission
method is called with a
ReflectPermission("suppressAccessChecks")
permission.
A SecurityException
is raised if flag
is
true
but accessibility of this object may not be changed
(for example, if this element object is a Constructor
object for
the class java.lang.Class
).
A SecurityException
is raised if this object is a java.lang.reflect.Constructor
object for the class
java.lang.Class
, and flag
is true.
boolean
on the specified object.
This method is equivalent to
set(obj, zObj)
,
where zObj
is a Boolean
object and
zObj.booleanValue() == z
.byte
on the specified object.
This method is equivalent to
set(obj, bObj)
,
where bObj
is a Byte
object and
bObj.byteValue() == b
.char
on the specified object.
This method is equivalent to
set(obj, cObj)
,
where cObj
is a Character
object and
cObj.charValue() == c
.double
on the specified object.
This method is equivalent to
set(obj, dObj)
,
where dObj
is a Double
object and
dObj.doubleValue() == d
.float
on the specified object.
This method is equivalent to
set(obj, fObj)
,
where fObj
is a Float
object and
fObj.floatValue() == f
.int
on the specified object.
This method is equivalent to
set(obj, iObj)
,
where iObj
is a Integer
object and
iObj.intValue() == i
.long
on the specified object.
This method is equivalent to
set(obj, lObj)
,
where lObj
is a Long
object and
lObj.longValue() == l
.short
on the specified object.
This method is equivalent to
set(obj, sObj)
,
where sObj
is a Short
object and
sObj.shortValue() == s
.Field
, including
its generic type. The format is the access modifiers for the
field, if any, followed by the generic field type, followed by
a space, followed by the fully-qualified name of the class
declaring the field, followed by a period, followed by the name
of the field.
The modifiers are placed in canonical order as specified by "The Java Language Specification". This is public, protected or private first, and then other modifiers in the following order: static, final, transient, volatile.
Field
. The format is
the access modifiers for the field, if any, followed
by the field type, followed by a space, followed by
the fully-qualified name of the class declaring the field,
followed by a period, followed by the name of the field.
For example:
public static final int java.lang.Thread.MIN_PRIORITY private int java.io.FileDescriptor.fd
The modifiers are placed in canonical order as specified by "The Java Language Specification". This is public, protected or private first, and then other modifiers in the following order: static, final, transient, volatile.
The current thread must own this object's monitor. The thread
releases ownership of this monitor and waits until another thread
notifies threads waiting on this object's monitor to wake up
either through a call to the notify
method or the
notifyAll
method. The thread then waits until it can
re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) { while (<condition does not hold>) obj.wait(); ... // Perform action appropriate to condition }This method should only be called by a thread that is the owner of this object's monitor. See the
notify
method for a
description of the ways in which a thread can become the owner of
a monitor.The current thread must own this object's monitor.
This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:
A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops, like this one:
synchronized (obj) { while (<condition does not hold>) obj.wait(timeout); ... // Perform action appropriate to condition }(For more information on this topic, see Section 3.2.3 in Doug Lea's "Concurrent Programming in Java (Second Edition)" (Addison-Wesley, 2000), or Item 50 in Joshua Bloch's "Effective Java Programming Language Guide" (Addison-Wesley, 2001).
If the current thread is interrupted by another thread while it is waiting, then an InterruptedException is thrown. This exception is not thrown until the lock status of this object has been restored as described above.
Note that the wait method, as it places the current thread into the wait set for this object, unlocks only this object; any other objects on which the current thread may be synchronized remain locked while the thread waits.
This method should only be called by a thread that is the owner
of this object's monitor. See the notify
method for a
description of the ways in which a thread can become the owner of
a monitor.
This method is similar to the wait
method of one
argument, but it allows finer control over the amount of time to
wait for a notification before giving up. The amount of real time,
measured in nanoseconds, is given by:
1000000*timeout+nanos
In all other respects, this method does the same thing as the method of one argument. In particular, wait(0, 0) means the same thing as wait(0).
The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until either of the following two conditions has occurred:
notify
method
or the notifyAll
method.
timeout
milliseconds plus nanos
nanoseconds arguments, has
elapsed.
The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) { while (<condition does not hold>) obj.wait(timeout, nanos); ... // Perform action appropriate to condition }This method should only be called by a thread that is the owner of this object's monitor. See the
notify
method for a
description of the ways in which a thread can become the owner of
a monitor.