Method Class | getDeclaringClass() method in Java
Last Updated :
11 Jul, 2025
The
java.lang.reflect.Method.getDeclaringClass() method of Method class returns the Class object for the class or interface which defines the method on which we are applying this getDeclaringClass() method.
Syntax:
public Class<?> getDeclaringClass()
Return Value: This method returns the
Class object in which method is declared.
Below program illustrates getDeclaringClass() method of Method class:
Program 1: To find the details of class Object which defined the method, by applying getDeclaringClass() method.
In below program a Demo class with some methods is created. After creating the class object, the list of method objects is created by calling getMethods() function of class object. Then they are looped through the list and the details of declaring class of each method are printed.
Output of this program also shows results for method objects other than methods defined in class object like wait, equals, toString, hashCode, getClass, notify, and notifyAll. These methods are inherited from superclass Object of java.lang package by class object.
Java
// Program Demonstrate getDeclaringClass() method
// of Method Class.
import java.lang.reflect.Method;
// sample class containing some method
class GFGDemoClass {
// create attributes
private String field1;
private String field2;
// create methods
public String getField1()
{
return field1;
}
public void setField1(String field1)
{
this.field1 = field1;
}
public String getField2()
{
return field2;
}
public void setField2(String field2)
{
this.field2 = field2;
}
}
// class containing the Main method
public class GFG {
public static void main(String[] args)
{
// get array Method objects of GFGDemoClass
Method[] methods = GFGDemoClass.class.getMethods();
// print getDeclaringclass for every Method object
// looping through a list of method objects
for (Method m : methods) {
// get a class object which declares the current method
// by declaringClass() method
Class declaringClass = m.getDeclaringClass();
// print Method name and class name
System.out.println("Method Name: " + m.getName());
System.out.println("Declaring class Name: "
+ declaringClass.getName());
}
}
}
Output:
Method Name: getField1
Declaring class Name: GFGDemoClass
Method Name: setField1
Declaring class Name: GFGDemoClass
Method Name: getField2
Declaring class Name: GFGDemoClass
Method Name: setField2
Declaring class Name: GFGDemoClass
Method Name: wait
Declaring class Name: java.lang.Object
Method Name: wait
Declaring class Name: java.lang.Object
Method Name: wait
Declaring class Name: java.lang.Object
Method Name: equals
Declaring class Name: java.lang.Object
Method Name: toString
Declaring class Name: java.lang.Object
Method Name: hashCode
Declaring class Name: java.lang.Object
Method Name: getClass
Declaring class Name: java.lang.Object
Method Name: notify
Declaring class Name: java.lang.Object
Method Name: notifyAll
Declaring class Name: java.lang.Object
Program 2: To find the details of Object class methods, by applying getDeclaringClass() method.
Java
// Program Demonstrate getDeclaringClass() method
// of Method Class.
import java.lang.reflect.Method;
// sample class containing some method
class BasicOperations {
// create attributes
int a;
int b;
// create methods
public int add()
{
return a + b;
}
public int multiply()
{
return a * b;
}
public int subtract()
{
return a - b;
}
public int division()
{
return a / b;
}
}
public class GFG {
public static void main(String[] args)
{
// get array Method objects
Method[] methods = BasicOperations.class.getMethods();
// print getDeclaringclass for every Method object
for (Method m : methods) {
// get class object by declaringClass() method
Class declaringClass = m.getDeclaringClass();
// checks whether the method belong to
// BasicOperations class or not
// and if yes then print method name
// along with declaring class name.
if (declaringClass.getName().equals("BasicOperations")) {
// print Method name and class name
System.out.println("Method Name: " + m.getName());
System.out.println("Declaring class Name: "
+ declaringClass.getName());
}
}
}
}
Output:
Method Name: multiply
Declaring class Name: BasicOperations
Method Name: subtract
Declaring class Name: BasicOperations
Method Name: division
Declaring class Name: BasicOperations
Method Name: add
Declaring class Name: BasicOperations
Reference: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getDeclaringClass--
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java