Skip to content
Related Articles

Related Articles

Field getLong() method in Java with Examples
  • Last Updated : 26 Aug, 2019

The getLong() method of java.lang.reflect.Field used to get the value of long which has to be static or instance field type. This method also used to get the value of another primitive type convertible to type long via a widening conversion. When a class contains a static or instance long 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 long getLong(Object obj)
             throws IllegalArgumentException,
                    IllegalAccessException

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

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

Exception: This method throws following Exception:



  1. IllegalAccessException: if Field object is enforcing Java language access control and the underlying field is inaccessible.
  2. IllegalArgumentException: 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 long by a widening conversion.
  3. NullPointerException: if the specified object is null and the field is an instance field.
  4. ExceptionInInitializerError: if the initialization provoked by this method fails.

Below programs illustrate getLong() method:
Program 1:




// Java program to demonstrate getLong() method
  
import java.lang.reflect.Field;
  
public class GFG {
  
    public static void main(String[] args)
        throws Exception
    {
  
        // Create the User class object
        User user = new User();
  
        // Get the marks field object
        Field field
            = User.class.getField("HighScore");
  
        // Apply getLong Method on User Object
        // to get the value of HighScore field
        long value = field.getLong(user);
  
        // print result
        System.out.println("Value of long Field"
                           + " HighScore is " + value);
    }
}
  
// sample User class
class User {
  
    // static long values
    public static long HighScore = 341313432299133L;
    public static String name = "Aman";
  
    public static long getHighScore()
    {
        return HighScore;
    }
  
    public static void setHighScore(long HighScore)
    {
        User.HighScore = HighScore;
    }
  
    public static String getName()
    {
        return name;
    }
  
    public static void setName(String name)
    {
        User.name = name;
    }
}
Output:
Value of long Field HighScore is 341313432299133

Program 2:




// Java program to demonstrate getLong() method
  
import java.lang.reflect.Field;
  
public class GFG {
  
    public static void main(String[] args)
        throws Exception
    {
  
        // Create the Numbers class object
        Numbers numbers = new Numbers();
  
        // Get the value field object
        Field field
            = Numbers.class.getField("value");
  
        // Apply getLong Method on field Object
        // to get the value of value field
        long value = field.getLong(numbers);
  
        // print result
        System.out.println("Value: " + value);
    }
  
    // Numbers class
    static class Numbers {
  
        // long field
        public static long value = 9999994567L;
  
        // getter and setter methods
        public static long getValue()
        {
            return value;
        }
  
        public static void setValue(long value)
        {
            Numbers.value = value;
        }
    }
}
Output:
Value: 9999994567

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :