Java Class getField() Method



Description

The Java Class getField() method returns a Field object that reflects the specified public member field of the class or interface represented by this Class object. The name parameter is a String specifying the simple name of the desired field.

Declaration

Following is the declaration for java.lang.Class.getField() method

public Field getField(String name) throws NoSuchFieldException, SecurityException

Parameters

name − This is the field name.

Return Value

This method returns the Field object of this class specified by name.

Exception

  • NoSuchFieldException − If a field with the specified name is not found.

  • NullPointerException − If name is null

  • SecurityException − If a security manager, s, is present.

Getting Field of a Class Example

The following example shows the usage of java.lang.Class.Field() method. In this program, we've created an instance of ClassDemo and then using getClass() method, the class of the instance is retrieved. Using Field(), we've retrieved required field and printed it.

package com.tutorialspoint;

public class ClassDemo {

   public static void main(String[] args) {
 
      ClassDemo c = new ClassDemo();
      Class cls = c.getClass();

      System.out.println("Field =");

      try {
         // string field
         Field sField = cls.getField("string1");
         System.out.println("Public field found: " + sField.toString());
      } catch(NoSuchFieldException e) {
         System.out.println(e.toString());
      }
   }

   public ClassDemo() {
      // no argument constructor
   }

   public ClassDemo(String string1) {       
      this.string1 = string1;
   }
    
   public String string1 = "tutorialspoint";
} 

Output

Let us compile and run the above program, this will produce the following result −

Field =
Public field found: public java.lang.String ClassDemo.string1

Getting Field of an ArrayList Class Example

The following example shows the usage of java.lang.Class.getField() method. In this program, we've used class of ArrayList. Using getField(), we've retrieved a non-existing Field and program throws an exception.

package com.tutorialspoint;

import java.lang.reflect.Field;
import java.util.ArrayList;

public class ClassDemo {

   public static void main(String[] args) {

      Class cls = ArrayList.class;
      System.out.println("Field =");

      try {
         // string field
         Field sField = cls.getField("size");
         System.out.println("Public field found: " + sField.toString());
      } catch(NoSuchFieldException e) {
         System.out.println(e.toString());
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Field =
java.lang.NoSuchFieldException: size

Getting Field of a Thread Class Example

The following example shows the usage of java.lang.Class.getField() method. In this program, we've used class of Thread. Using getField(), we've retrieved an existing Field and result is printed.

package com.tutorialspoint;

import java.lang.reflect.Field;

public class ClassDemo {

   public static void main(String[] args) {

      Class cls = Thread.class;
      System.out.println("Field =");

      try {
         // string field
         Field sField = cls.getField("MIN_PRIORITY");
         System.out.println("Public field found: " + sField.toString());
      } catch(NoSuchFieldException e) {
         System.out.println(e.toString());
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Field =
Public field found: public static final int java.lang.Thread.MIN_PRIORITY
java_lang_class.htm
Advertisements