Class Field in Java

Class Field in Java
Spread the love

Class Field in Java

Field class represents a field of a class. Instances of Field are obtained by calling the getField() and related methods of java.lang.Class. Field implements the Member interface, so once we have obtained a Field object, we can use getName(), getModifiers(), and getDeclaringClass() to determine the name, modifiers, and class of the field. Additionally, getType() returns the type of the field.

The set() method sets the value of the represented field for a specified object to a given value. (If the represented field is static, then no object need be specified for it to be set upon, of course.) If the field is of a primitive type, its value can be specified using a wrapper object of type Boolean, Integer, and so on, or it can be set using the setBoolean(), setInt(), and related methods.

Similarly, the get() method queries the value of the represented field for a specified object and returns the field value as an Object. Various other methods query the field value and return it as various primitive types.

The Field class represents a variable or constant in a class. A Field object can be obtained by calling the getField() method of a Class object. Field includes methods for getting the name, modifiers, type, and declaring class of a field. The class also provides methods that can set and retrieve the value of a field for a particular object.

The structure of the class Field is given by

public final class java.lang.reflect.Field extends java.lang.Object implements java.lang.reflect.Member {
// No Constructor
// Public Instance Methods
public boolean equals(Object obj); // Overrides Object
public native Object get(Object obj) throws IllegalArgumentException,IllegalAccessException;
public native boolean getBoolean(Object obj) throws IllegalArgumentException, IllegalAccessException;
public native byte getByte(Object obj) throws IllegalArgumentException, IllegalAccessException;
public native char getChar(Object obj) throws IllegalArgumentException, IllegalAccessException;
public Class getDeclaringClass(); // From Member
public native double getDouble(Object obj) throws IllegalArgumentException, IllegalAccessException;
public native float getFloat(Object obj) throws IllegalArgumentException,IllegalAccessException;
public native int getInt(Object obj) throws IllegalArgumentException,IllegalAccessException;
public native long getLong(Object obj) throws IllegalArgumentException,IllegalAccessException;
public native int getModifiers(); // From Member
public String getName(); // From Member
public native short getShort(Object obj) throws IllegalArgumentException,IllegalAccessException;
public Class getType();
public int hashCode(); // Overrides Object
public native void set(Object obj, Object value) throws IllegalArgumentException, IllegalAccessException;
public native void setBoolean(Object obj, boolean z) throws IllegalArgumentException, IllegalAccessException;
public native void setByte(Object obj, byte b) throws IllegalArgumentException, IllegalAccessException;
public native void setChar(Object obj, char c) throws IllegalArgumentException, IllegalAccessException;
public native void setDouble(Object obj, double d) throws IllegalArgumentException, IllegalAccessException;
public native void setFloat(Object obj, float f) throws IllegalArgumentException, IllegalAccessException;
public native void setInt(Object obj, int i) throws IllegalArgumentException, IllegalAccessException;
public native void setLong(Object obj, long l) throws IllegalArgumentException, IllegalAccessException;
public native void setShort(Object obj, short s) throws IllegalArgumentException, IllegalAccessException;
public String toString(); // Overrides Object
}

The details of the class structure are given as follows:

public boolean equals(Object obj);

public boolean equals(Object obj) method returns true if obj is an instance of Field, and it is equivalent to this Field.

This method returns true if the objects are equal; false if they are not.

Parameter
obj – The object to be compared with this object.

public native Object get(Object obj);

public native Object get(Object obj) method returns the value of this field in the given object. If the field is declared static, the obj parameter is ignored. Otherwise, the object supplied must be an instance of the class that declares this field.

If the field contains a value of a primitive type, the value is wrapped in an appropriate object, and the object is returned.

This method returns the value of this field in the given object.

Parameter
obj – the instance whose field value is to be retrieved.

public native boolean getBoolean(Object obj);

public native boolean getBoolean(Object obj) method returns the value of this field in the given object as a boolean. If the field is declared static, the obj parameter is ignored.

Otherwise, the object supplied must be an instance of the class that declares this field.

This method returns the boolean value of this field in the given object.

Parameter
obj – The instance whose field value is to be retrieved.

public native byte getByte(Object obj);

public native byte getByte(Object obj) method returns the value of this field in the given object as a byte. If the field is declared static, the obj parameter is ignored. Otherwise, the object supplied must be an instance of the class that declares this field.

This method returns the byte value of this field in the given object.

Parameter
obj – The instance whose field value is to be retrieved.

public native char getChar(Object obj);

public native char getChar(Object obj) method returns the value of this field in the given object as a char. If the field is declared static, the obj parameter is ignored. Otherwise, the object supplied must be an instance of the class that declares this field.

This method returns The char value of this field in the given object.

Parameter
obj – the instance whose field value is to be retrieved.

public Class getDeclaringClass();

public Class getDeclaringClass() method returns the Class object for the class in which this field is declared.

This method returns the Class object that represents the class that declared this field.

public native double getDouble(Object obj);

public native double getDouble(Object obj) method returns the value of this field in the given object as a double. If the field is declared static, the obj parameter is ignored. Otherwise, the object supplied must be an instance of the class that declares this field.

This method returns the double value of this field in the given object.

Parameter
obj – The instance whose field value is to be retrieved.

public native float getFloat(Object obj);

public native float getFloat(Object obj) method returns the value of this field in the given object as a float. If the field is declared static, the obj parameter is ignored. Otherwise, the object supplied must be an instance of the class that declares this field.

This method returns the float value of this field in the given object.

Parameter
obj – The instance whose field value is to be retrieved.

public native int getInt(Object obj);

public native int getInt(Object obj) method returns the value of this field in the given object as an int. If the field is declared static, the obj parameter is ignored. Otherwise, the object supplied must be an instance of the class that declares this field.

This method returns the int value of this field in the given object.

Parameter
obj – The instance whose field value is to be retrieved.

public native long getLong(Object obj);

public native long getLong(Object obj) method returns the value of this field in the given object as a long. If the field is declared static, the obj parameter is ignored. Otherwise, the object supplied must be an instance of the class that declares this field.

This method returns the long value of this field in the given object.

Parameter
obj – The instance whose field value is to be retrieved.

public native int getModifiers();

public native int getModifiers() method returns an integer value that represents the modifiers of this field. The Modifier class should decode the returned value.

This method returns an integer that represents the modifier keywords used to declare this field.

public String getName();

public String getName() method returns the name of this field.

This method returns the name of this field as a String.

public native short getShort(Object obj);

public native short getShort(Object obj) method returns the value of this field in the given object as a short. If the field is declared static, the obj parameter is ignored. Otherwise, the object supplied must be an instance of the class that declares this field.

This method returns the short value of this field in the given object.

Parameter
obj – The instance whose field value is to be retrieved.

public Class getType();

public Class getType() method returns the Class object for the type of this field.

This method returns the Class object that represents the type of this field.

public int hashCode();

public int hashCode() method returns a hashcode for this Field.

This method returns a hashcode for this object.

public native void set(Object obj, Object value);

public native void set(Object obj, Object value) method sets the value of this field in the given object to the given value. If the field is declared static, the obj parameter is ignored. Otherwise, the object supplied must be an instance of the class that declares this field.

If the field contains a value of a primitive type, the given value is automatically unwrapped before it is used to set the value of the field.

Parameter
obj – The instance whose field value is to be set.
value – The new value.

public native void setBoolean(Object obj, boolean z);

public native void setBoolean(Object obj, boolean z) method sets the value of this field in the given object to the given boolean value. If the field is declared static, the obj parameter is ignored. Otherwise, the object supplied must be an instance of the class that declares this field.

Parameter
obj – The instance whose field value is to be set.
z – The new value.

public native void setByte(Object obj, byte b);

public native void setByte(Object obj, byte b) method sets the value of this field in the given object to the given byte value. If the field is declared static, the obj parameter is ignored.

Otherwise, the object supplied must be an instance of the class that declares this field.

Parameter
obj – The instance whose field value is to be set.
b – The new value.

public native void setChar(Object obj, char c);

public native void setChar(Object obj, char c) method sets the value of this field in the given object to the given char value. If the field is declared static, the obj parameter is ignored.

Otherwise, the object supplied must be an instance of the class that declares this field.

Parameter
obj – The instance whose field value is to be set.
c – The new value.

public native void setDouble(Object obj, double d);

public native void setDouble(Object obj, double d) method sets the value of this field in the given object to the given double value. If the field is declared static,the obj parameter is ignored. Otherwise, the object supplied must be an instance of the class that declares this field.

Parameter
obj – The instance whose field value is to be set.
d – The new value.

public native void setFloat(Object obj, float f);

public native void setFloat(Object obj, float f) method sets the value of this field in the given object to the given float value. If the field is declared static, the obj parameter is ignored. Otherwise, the object supplied must be an instance of the class that declares this field.

Parameter
obj – The instance whose field value is to be set.
f – The new value.

public native void setInt(Object obj, int i);

public native void setInt(Object obj, int i) method sets the value of this field in the given object to the given int value. If the field is declared static, the obj parameter is ignored. Otherwise, the object supplied must be an instance of the class that declares this field.

Parameter
obj – The instance whose field value is to be set.
i – The new value.

public native void setLong(Object obj, long l);

public native void setLong(Object obj, long l) method sets the value of this field in the given object to the given long value. If the field is declared static, the obj parameter is ignored. Otherwise, the object supplied must be an instance of the class that declares this field.

Parameter
obj – The instance whose field value is to be set.
l – The new value.

public native void setShort(Object obj, short s);

public native void setShort(Object obj, short s) method sets the value of this field in the given object to the given short value. If the field is declared static, the obj parameter is ignored. Otherwise, the object supplied must be an instance of the class that declares this field.

Parameter
obj – The instance whose field value is to be set.
s – The new value.

public String toString();

public String toString() method returns a string representation of this Field. This string contains the access modifiers of the field, if any, followed by the type, the fully qualified name of the declaring class, a period, and the name of the field.

This method returns a string representation of this object.

Apart from these, Field class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.


Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Comment

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

Scroll to Top