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.
import java.lang.reflect.Method;
class GFGDemoClass {
private String field1;
private String field2;
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;
}
}
public class GFG {
public static void main(String[] args)
{
Method[] methods = GFGDemoClass. class .getMethods();
for (Method m : methods) {
Class declaringClass = m.getDeclaringClass();
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.
import java.lang.reflect.Method;
class BasicOperations {
int a;
int b;
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)
{
Method[] methods = BasicOperations. class .getMethods();
for (Method m : methods) {
Class declaringClass = m.getDeclaringClass();
if (declaringClass.getName().equals( "BasicOperations" )) {
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–