Close

Java Reflection - Field.getLong() Examples

Java Reflection Java Java API 


Class:

java.lang.reflect.Field

java.lang.Objectjava.lang.Objectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AnnotatedElementAnnotatedElementjava.lang.reflect.Fieldjava.lang.reflect.Fieldjava.lang.reflect.MemberMemberLogicBig

Method:

public long getLong(Object obj)
             throws IllegalArgumentException,
                    IllegalAccessException

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.


Examples


package com.logicbig.example.field;

import java.lang.reflect.Field;

public class GetLongExample {
private long l = 20;

public static void main(String... args) throws NoSuchFieldException, IllegalAccessException {
Field f = GetLongExample.class.getDeclaredField("l");
long value = f.getLong(new GetLongExample());
System.out.println(value);
}
}

Output

20




Widening conversion from double to long:

package com.logicbig.example.field;

import java.lang.reflect.Field;

public class GetLongExample2 {
private int i = 100;

public static void main(String... args) throws NoSuchFieldException, IllegalAccessException {
Field field = GetLongExample2.class.getDeclaredField("i");
long v = field.getLong(new GetLongExample2());
System.out.println(v);
}
}

Output

100




Static field example:

package com.logicbig.example.field;

import java.lang.reflect.Field;

public class GetLongExample3 {
private static long l = 100;

public static void main(String... args) throws NoSuchFieldException, IllegalAccessException {
Field f = GetLongExample3.class.getDeclaredField("l");
long aLong = f.getLong(null);
System.out.println(aLong);
}
}

Output

100




See Also