Introduction

The java.lang.reflect.Field class 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.

Class declaration

Following is the declaration for java.lang.reflect.Field class −

public final class Field
   extends AccessibleObject
      implements Member

Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

Class methods

Sr.No.Method & Description
1boolean equals(Object obj)Compares this Field against the specified object.
2Object get(Object obj)Returns the value of the field represented by this Field, on the specified object.
3<T extends Annotation> T getAnnotation(Class<T> annotationClass)Returns this element’s annotation for the specified type if such an annotation is present, else null.
4boolean getBoolean(Object obj)Gets the value of a static or instance boolean field.
5byte getByte(Object obj)Gets the value of a static or instance byte field.
6char getChar(Object obj)Gets the value of a static or instance field of type char or of another primitive type convertible to type char via a widening conversion.
7Annotation[] getDeclaredAnnotations()Returns all annotations that are directly present on this element.
8Class<?> getDeclaringClass()Returns the Class object representing the class or interface that declares the field represented by this Field object.
9double getDouble(Object obj)Gets the value of a static or instance field of type double or of another primitive type convertible to type double via a widening conversion.
10float getFloat(Object obj)Gets the value of a static or instance field of type float or of another primitive type convertible to type float via a widening conversion.
11Type getGenericType()Returns a Type object that represents the declared type for the field represented by this Field object.
12int getInt(Object obj)Gets the value of a static or instance field of type int or of another primitive type convertible to type int via a widening conversion.
13long getLong(Object obj)Gets the value of a static or instance field of type long or of another primitive type convertible to type long via a widening conversion.
14int getModifiers()Returns the Java language modifiers for the field represented by this Field object, as an integer.
15String getName()RReturns the name of the field represented by this Field object.
16short getShort(Object obj)Gets the value of a static or instance field of type short or of another primitive type convertible to type short via a widening conversion.
17Class<?> getType()Returns a Class object that identifies the declared type for the field represented by this Field object.
18int hashCode()Returns a hashcode for this Field.
19boolean isEnumConstant()Returns true if this field represents an element of an enumerated type; returns false otherwise.
20boolean isSynthetic()This method returns true if the field represented by the current object is synthetic, else it returns false.
21void setBoolean(Object obj, boolean z)Sets the value of a field as a boolean on the specified object.
22void setByte(Object obj, byte b)Sets the value of a field as a byte on the specified object.
23void setChar(Object obj, char c)Sets the value of a field as a char on the specified object.
24void setDouble(Object obj, double d)Sets the value of a field as a double on the specified object.
25void setFloat(Object obj, float f)Sets the value of a field as a float on the specified object.
26void setInt(Object obj, int i)Sets the value of a field as an int on the specified object.
27void setLong(Object obj, long l)Sets the value of a field as a long on the specified object.
28void setShort(Object obj, short s)Sets the value of a field as a short on the specified object.
29String toGenericString()Returns a string describing this Field, including its generic type.
30String toString()Returns a string describing this Field.

Methods inherited

This class inherits methods from the following classes −

  • java.lang.reflect.AccessibleObject
  • java.lang.Object

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *