Field setByte() method in Java with Examples
setByte() method of java.lang.reflect.Field used to set the value of a field as a byte on the specified object. When you need to set the value of a field of an object as byte then you can use this method to set value over an Object.
Syntax:
public void setByte(Object obj, byte b)
throws IllegalArgumentException,
IllegalAccessException
Parameters: This method accepts accepts two parameters:
- obj: which is the object whose field should be modified and
- b: which is the new value for the field of obj being modified.
Return: This method returns nothing.
Exception: This method throws the following Exception:
- IllegalAccessException: if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final.
- IllegalArgumentException: if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementor thereof), or if an unwrapping conversion fails.
- NullPointerException: if the specified object is null and the field is an instance field.
- ExceptionInInitializerError: if the initialization provoked by this method fails.
Below programs illustrate setByte() method:
Program 1:
// Java program to illustrate setByte() method import java.lang.reflect.Field; public class GFG { public static void main(String[] args) throws Exception { // create user object Employee emp = new Employee(); // print value of leaveLeft System.out.println( "Value of leaveLeft before " + "applying setByte is " + emp.leaveLeft); // Get the marks field object Field field = Employee.class .getField("leaveLeft"); // Apply setByte Method field.setByte(emp, (byte)2); // print value of leaveLeft System.out.println( "Value of leaveLeft after " + "applying setByte is " + emp.leaveLeft); // print value of age System.out.println( "Value of age before " + "applying setByte is " + emp.age); // Get the marks field object field = Employee.class.getField("age"); // Apply setByte Method field.setByte(emp, (byte)56); // print value of age System.out.println( "Value of age after " + "applying setByte is " + emp.age); } } // sample class class Employee { // static byte values public static byte age = 45; public static byte leaveLeft = 12; } |
Value of leaveLeft before applying setByte is 12 Value of leaveLeft after applying setByte is 2 Value of age before applying setByte is 45 Value of age after applying setByte is 56
Program 2:
// Java program to illustrate setByte() method import java.lang.reflect.Field; public class GFG { public static void main(String[] args) throws Exception { // create user object User user = new User(); // Get the id field object Field field = User.class .getField("id"); // Apply setByte Method field.setByte(user, (byte)20); // print value of isActive System.out.println( "Value after " + "applying setByte is " + user.id); } } // sample User class class User { // static byte values public static byte id = 2; } |
Value after applying setByte is 20
Recommended Posts:
- Array setByte() method in Java
- Field get() method in Java with Examples
- Field set() method in Java with Examples
- Field setShort() method in Java with Examples
- Field equals() method in Java with Examples
- Field getBoolean() method in Java with Examples
- Field getAnnotation() method in Java With Examples
- Field isSynthetic() method in Java with Examples
- Field getDeclaringClass() method in Java with Examples
- Field getChar() method in Java with Examples
- Field getModifiers() method in Java with Examples
- Field isEnumConstant() method in Java with Examples
- Field getByte() method in Java with Examples
- Field getDeclaredAnnotations() method in Java With Examples
- Field getDouble() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



