How to get all Methods available in a Class
Points To Remember
  • We can use Reflection API to get all methods available in a class.
  • Method class is present in java.land.reflect.Method class.
  • We can use getDeclaredMethods() to get all the declared Methods in a class.
  • We can use getMethods() to get all the methods available in a class. It also includes methods from its super classes.
Program : Get All Methods Declared in a Class
import java.lang.reflect.Method;


class SampleClass{

 String s1 = "Class variable";
 int a = 123;

 public SampleClass(){
  System.out.println("SampleClass Default Constructor");
 }

 public SampleClass(String str){
  System.out.println("SampleClass Overloaded Constructor");
 }

 public void show(){
  System.out.println("SampleClass Show Method");
 }

 public void print(){
  System.out.println("SampleClass Print Method");
 }

}

class Test{

 public static void main(String args[]){
   Test obj = new Test();
   try{
 Class clazz = Class.forName("SampleClass");
        Method declaredMethods[] = obj.getDeclaredMethods(clazz);
        for(Method method : declaredMethods){
          System.out.println(method);
        }
   }catch(Exception e){
 e.printStackTrace();
   }
   
 }

  public Method[] getDeclaredMethods(Class clazz){
    return clazz.getDeclaredMethods(); 
 }

  

}
public void SampleClass.show()
public void SampleClass.print()
Program : Get All Declared and Inherited Methods of a Class
import java.lang.reflect.Method;


class SampleClass{

 String s1 = "Class variable";
 int a = 123;

 public SampleClass(){
  System.out.println("SampleClass Default Constructor");
 }

 public SampleClass(String str){
  System.out.println("SampleClass Overloaded Constructor");
 }

 public void show(){
  System.out.println("SampleClass Show Method");
 }

 public void print(){
  System.out.println("SampleClass Print Method");
 }

}

class Test{

 public static void main(String args[]){
   Test obj = new Test();
   try{
 Class clazz = Class.forName("SampleClass");
 Method allMethods[] = obj.getAllMethods(clazz);
        for(Method method : allMethods){
          System.out.println(method);
        }
   }catch(Exception e){
 e.printStackTrace();
   }
   
 }

  public Method[] getAllMethods(Class clazz){
    return clazz.getMethods(); 
 }

  

}
public void SampleClass.show()
public void SampleClass.print()
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
Difference between the getDeclaredMethods() and getMethods() is that getDeclaredMethods() will return all the methods that are declared within the class while getMethods() will return all the methods that are declared in the class and all the methods that this class inherits from its super classes.

No comments :

Post a Comment