Close

Java Reflection - Field.getShort() 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 short getShort(Object obj)
               throws IllegalArgumentException,
                      IllegalAccessException

Gets the value of a static or instance field of type short or of another primitive type convertible to type short via a widening conversion.


Examples


package com.logicbig.example.field;

import java.lang.reflect.Field;

public class GetShortExample {
private short aShort = 3;

public static void main(String... args) throws NoSuchFieldException, IllegalAccessException {
Field f = GetShortExample.class.getDeclaredField("aShort");
short v = f.getShort(new GetShortExample());
System.out.println(v);
}
}

Output

3




Widening conversion:

package com.logicbig.example.field;

import java.lang.reflect.Field;

public class GetShortExample2 {
private byte aByte = 100;

public static void main(String... args) throws NoSuchFieldException, IllegalAccessException {
Field f = GetShortExample2.class.getDeclaredField("aByte");
short v = f.getShort(new GetShortExample2());
System.out.println(v);
}
}

Output

100




Static field example:

package com.logicbig.example.field;

import java.lang.reflect.Field;

public class GetShortExample3 {
private static short aShort = 5;

public static void main(String... args) throws NoSuchFieldException, IllegalAccessException {
Field f = GetShortExample3.class.getDeclaredField("aShort");
short v = f.getShort(null);
System.out.println(v);
}
}

Output

5




See Also