Friday, October 10, 2025
HomeLanguagesJavaField getDeclaringClass() method in Java with Examples

Field getDeclaringClass() method in Java with Examples

The getDeclaringClass() method of java.lang.reflect.Field is used to get the Class object which declares the field represented by this Field object. If Field object is present and we want to get the class object then we can get that class object using this method.

Syntax:

public Class<T> getDeclaringClass()

Parameters: This method accepts nothing.

Return value: This method returns an object representing the declaring class of the underlying member.

Below programs illustrate getDeclaringClass() method:
Program 1:




// Java program to demonstrate the above method
  
import java.lang.reflect.Field;
  
public class GFG {
  
    public static void main(String[] args)
        throws NoSuchFieldException,
               SecurityException
    {
  
        // Get the value field object
        Field field
            = User.class
                  .getField("identificationChar");
  
        // get the declaring class object
        Class declaringClass
            = field.getDeclaringClass();
  
        // print result
        System.out.println("Declaring Class"
                           + " for Field Object: "
                           + declaringClass);
    }
}
  
// sample User class
class User {
  
    // static char values
    public static char identificationChar = 'E';
    public static char selectionChar = 'A';
    public static String name = "Aman";
  
    // getter and setter methods
    public static char getIdentificationChar()
    {
        return identificationChar;
    }
  
    public static void
    setIdentificationChar(char identificationChar)
    {
        User.identificationChar = identificationChar;
    }
  
    public static char getSelectionChar()
    {
        return selectionChar;
    }
  
    public static void
    setSelectionChar(char selectionChar)
    {
        User.selectionChar = selectionChar;
    }
  
    public static String getName()
    {
        return name;
    }
  
    public static void setName(String name)
    {
        User.name = name;
    }
}


Output:

Declaring Class for Field Object: class User

Program 2:




// Java program to demonstrate the above method
  
import java.lang.reflect.Field;
  
import java.lang.reflect.Field;
  
public class GFG {
  
    public static void main(String[] args)
        throws NoSuchFieldException,
               SecurityException
    {
  
        // Get the value field object
        Field field
            = Alphabets.class.getField("value");
  
        // get the declaring class object
        Class declaringClass
            = field.getDeclaringClass();
  
        // print result
        System.out.println("Declaring Class: "
                           + declaringClass);
    }
  
    // Alphabets class
    static class Alphabets {
  
        // char field
        public static char value = 'Q';
  
        // getter and setter methods
        public static char getValue()
        {
            return value;
        }
  
        public static void setValue(char value)
        {
            Alphabets.value = value;
        }
    }
}


Output:

Declaring Class: class GFG$Alphabets

References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getDeclaringClass–

RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS