Field getShort() method in Java with Examples Last Updated : 26 Aug, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report The getShort() method of java.lang.reflect.Field used to get the value of short which has to be static or instance field type. This method also used to get the value of another primitive type convertible to type short via a widening conversion. When a class contains a static or instance short 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 short getShort(Object obj) throws IllegalArgumentException, IllegalAccessException Parameters: This method accepts a single parameter obj which is the object to extract the short value from. Return value: This method returns the value of field converted to type short. Exception: This method throws following Exception: IllegalAccessException: if Field object is enforcing Java language access control and the underlying field is inaccessible. 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 short by a widening conversion. 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 getShort() method: Program 1: Java // Java program to demonstrate getShort() 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("Marks"); // Apply getShort Method on User Object // to get the value of Marks field short value = field.getShort(user); // print result System.out.println("Value of short Field" + " Marks is " + value); } } // sample User class class User { // static short values public static short Marks = 34; public static String name = "Aman"; public static short getMarks() { return Marks; } public static void setMarks(short marks) { Marks = marks; } public static String getName() { return name; } public static void setName(String name) { User.name = name; } } Output: Value of short Field Marks is 34 Program 2: Java // Java program to demonstrate getShort() method import java.lang.reflect.Field; public class GFG { public static void main(String[] args) throws Exception { // Create the SmallerNumbers class object SmallerNumbers smallNo = new SmallerNumbers(); // Get the value field object Field field = SmallerNumbers.class .getField("value"); // Apply getShort Method on field Object // to get the value of value field short value = field.getShort(smallNo); // print result System.out.println("Value: " + value); } // SmallerNumbers class static class SmallerNumbers { // short field public static short value = 999; // getter and setter methods public static short getValue() { return value; } public static void setValue(short value) { SmallerNumbers.value = value; } } } Output: Value: 999 References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getShort-java.lang.Object- Comment More infoAdvertise with us Next Article Field get() method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions java-lang-reflect-package Java-Field Practice Tags : Java Similar Reads Field get() method in Java with Examples The get() method of java.lang.reflect.Field used to get the value of the field object. If Field has a primitive type then the value of the field is automatically wrapped in an object. If the field is a static field, the argument of obj is ignored; it may be null Otherwise, the underlying field is an 3 min read Field getInt() method in Java with Examples The getInt() method of java.lang.reflect.Field used to get the value of int which has to be static or instance field type. This method also used to get the value of another primitive type convertible to type int via a widening conversion. When a class contains a static or instance int field and we w 3 min read Field getType() method in Java with Examples The getType() method of java.lang.reflect.Field used to get the declared type of the field represented by this Field object.This method returns a Class object that identifies the declared type Syntax: public String getType() Parameters: This method accepts nothing. Return value: This method returns 2 min read Field getLong() method in Java with Examples 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 3 min read Field getModifiers() method in Java with Examples The getModifiers () method of java.lang.reflect.Field used to return the modifiers used for the field object as time of declaration, as an integer. The Modifier class should be used to decode the modifiers. Syntax: public int getModifiers() Parameters: This method accepts nothing. Return: This metho 1 min read Field getName() method in Java with Examples The getName() method of java.lang.reflect.Field used to get the name of the field represented by this Field object. When a class contains a field and we want to get the name of that field then we can use this method to return the name of Field. Syntax: public String getName() Parameters: This method 3 min read Like