Field getByte() method in Java with Examples

The getByte() method of java.lang.reflect.Field used to get the value of byte which has to be static or instance field type. When a class contains a static or instance byte field and we want to get the value of that field then we can use this method to return the value of Field.

Syntax:

public Byte getByte(Object obj)
            throws IllegalArgumentException,
                   IllegalAccessException

Parameters: This method accepts a single parameter obj which is the object to extract the byte value from.



Return value: This method returns the value of field converted to type byte.

Exception: This method throws following Exception:

  1. IllegalAccessException: This exception is thrown if Field object is enforcing Java language access control and the underlying field is inaccessible.
  2. IllegalArgumentException: This exception is thrown if the specified object is not an instance of the class or interface declaring the underlying field or if the field value cannot be converted to the type byte by a widening conversion.
  3. NullPointerException: This exception is thrown if the specified object is null and the field is an instance field.
  4. ExceptionInInitializerError: This exception is thrown if the initialization provoked by this method fails.

Below programs illustrate getByte() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the getByte() method
  
import java.lang.reflect.Field;
  
public class GFG {
  
    public static void main(String[] args)
        throws NoSuchFieldException,
               SecurityException,
               IllegalArgumentException,
               IllegalAccessException
    {
  
        // Create the User class object
        User user = new User();
  
        // Get the identificationByte field object
        Field field
            = User.class
                  .getField("identificationByte");
  
        // Apply getByte Method on User Object
        // to get the value of identificationByte field
        byte value = field.getByte(user);
  
        // print result
        System.out.println("Value of Byte Field"
                           + " identificationByte is "
                           + value);
  
        // Now Get the selectionByte field object
        field = User.class.getField("selectionByte");
  
        // Apply getByte Method on User Object
        // to get the value of selectionByte field
        value = field.getByte(user);
  
        // print result
        System.out.println("Value of Byte Field"
                           + " selectionByte is "
                           + value);
    }
}
  
// sample User class
class User {
  
    // static Byte values
    public static byte identificationByte = 'E';
    public static byte selectionByte = 121;
  
    // getter and setter methods
    public static byte getIdentificationByte()
    {
        return identificationByte;
    }
  
    public static void
    setIdentificationByte(byte identificationByte)
    {
        User.identificationByte = identificationByte;
    }
  
    public static byte getSelectionByte()
    {
        return selectionByte;
    }
    public static void
    setSelectionByte(byte selectionByte)
    {
        User.selectionByte = selectionByte;
    }
}

chevron_right


Output:

Value of Byte Field identificationByte is 69
Value of Byte Field selectionByte is 121

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the getByte() method
  
import java.lang.reflect.Field;
  
import java.lang.reflect.Field;
  
public class GFG {
  
    public static void main(String[] args)
        throws NoSuchFieldException,
               SecurityException,
               IllegalArgumentException,
               IllegalAccessException
    {
  
        // Create the Codes class object
        Codes codes = new Codes();
  
        // Get the value field object
        Field field
            = Codes.class.getField("value");
  
        // Apply getByte Method on field Object
        // to get the value of value field
        byte value = field.getByte(codes);
  
        // print result
        System.out.println("Value: " + value);
    }
}
// Codes class
class Codes {
    // Byte field
    public static byte value = 12;
}

chevron_right


Output:

Value: 12

References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getByte-java.lang.Object-



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

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.