Home » Java programming language

Java Class class getField() method with example

Class class getField() method: Here, we are going to learn about the getField() method of Class class with its syntax and example.
Submitted by Preeti Jain, on November 14, 2019

Class class getField() method

  • getField() method is available in java.lang package.
  • getField() method is used to return a Field objects that indicate the given public member field of the class or an interface denoted by this Class object.
  • getField() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getField() method may throw an exception at the time of returning a Field object.
    • NoSuchFieldException: In this exception when a specifying field does not exist.
    • SecurityException: In this exception, it may raise when the security manager exists.
    • NullPointerException: In this exception when the given field is null.


    public Field getField (String field_name);


  • String field_name – represents the name of the field.

Return value:

The return type of this method is Field, it returns Field object of the given Field in this Class.


// Java program to demonstrate the example 
// of Field getField (String field_name) method of Class 

import java.lang.reflect.*;

public class GetFieldOfClass {
    public static void main(String[] args) throws Exception {
        GetFieldOfClass field = new GetFieldOfClass();

        // Get Class
        Class cl = field.getClass();

        // By using getField() method is to get the public field of
        // the class 
        Field f = cl.getField("i");
        System.out.println("Field: " + f.toString());

    // Private Constructors
    private GetFieldOfClass() {
        System.out.println("We are in private constructor");

    // Public Constructors
    public GetFieldOfClass(int i) {
        this.i = i;

    public int i = 100;


We are in private constructor
Field: public int GetFieldOfClass.i

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.