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



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.