Open In App

java.lang.reflect.Method Class in Java

Last Updated : 10 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

java.lang.reflect.Method class provides necessary details about one method on a certain category or interface and provides access for the same. The reflected method could also be a category method or an instance method (including an abstract method).

This class allows broadening of conversions to occur when matching the actual parameters to call the underlying method’s formal parameters, but if narrowing conversion would occur, it throws an IllegalArgumentException.

java.lang.Object

            java.lang.reflect.AccessibleObject

                        java.lang.reflect.Executable

                                    java.lang.reflect.Method

Methods:

Method Description
equals(Object obj) This method compares this Method against the specified object.
getAnnotatedReturnType() This method returns an Annotated Type object that represents the use of a type to specify the return type of the method/constructor represented by this Executable.
getAnnotation(Class<T> annotationClass) This method returns this element’s annotation for the specified type if such an annotation is present, else null.
getDeclaredAnnotations() This method returns annotations that are directly present on this element.
getDeclaringClass() This method returns the Class object representing the class or interface that declares the executable represented by this object.
getDefaultValue() This method returns the default value for the annotation member represented by this Method instance.
getExceptionTypes() This method returns an array of Class objects that represent the types of exceptions declared to be thrown by the underlying executable represented by this object.
getGenericExceptionTypes() This method returns an array of Type objects that represent the exceptions declared to be thrown by this executable object.
getGenericParameterTypes() This method returns an array of Type objects that represent the formal parameter types, in declaration order, of the executable represented by this object.
getGenericReturnType() This method returns a Type object that represents the formal return type of the method represented by this Method object.
getModifiers() This method returns the Java language modifiers for the executable represented by this object.
getName() This method returns the name of the method represented by this Method object, as a String.
getParameterAnnotations() This method returns an array of arrays of Annotations that represent the annotations on the formal parameters, in declaration order, of the Executable represented by this object.
getParameterCount() This method returns the number of formal parameters (whether explicitly declared or implicitly declared or neither) for the executable represented by this object.
getParameterTypes() This method returns an array of Class objects that represent the formal parameter types, in declaration order, of the executable represented by this object.

getDefaultValue() Method:

Java




// Java program to show the
// Implementation of getDefaultvalue()
  
import java.lang.reflect.Method;
  
public class getDefaultValueExample {
  
    public static void main(String[] args)
    {
  
        Method[] methods = Demo.class.getMethods();
  
        // calling method getDefaultValue()
        System.out.println(methods[0].getDefaultValue());
    }
}
  
class Demo {
    private String str;
  
    // member function returning string str
    public String getSampleField() { return str; }
  
    public void setSampleField(String str)
    {
        this.str = str;
    }
}


Output

null

toString() Method:

Java




// Java program to show the
// Implementation of toString()
  
import java.lang.reflect.Method;
  
public class MethodDemo {
    public static void main(String[] args)
    {
  
        Method[] methods = SampleClass.class.getMethods();
  
        // calling method toString()
        System.out.println(methods[1].toString());
    }
}
  
class SampleClass {
    private String str;
  
    public String getSampleField() { return str; }
  
    public void setSampleField(String str)
    {
        this.str = str;
    }
}


Output

public void SampleClass.setSampleField(java.lang.String)


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads