How do I list public methods of a class using Reflection?

Using Java Reflection you can interrogate any class and find out all the public methods that are available to you. When you call the getMethods() method, it will return to you a Method[] array with the objects of methods that have been public in the class or interface. This method will also return all public methods of those inherited from superclasses or superinterfaces.

Getting All Public Methods for a given Class

Mammal dog = new Mammal();
Class dogClass  = dog.getClass();
Method[] publicMethods = dogClass.getMethods();

Calling the getMethods() method will return to you a Method[] array with the objects of methods that have been public in the class.

Iterating through all the Public Methods

for (Method method : publicMethods) {
  int mods = method.getModifiers();
   
  System.out.println("  PUBLIC METHODS are " + method.getName() 
       + " mods=" + Modifier.toString(mods)
       + " retType=" + method.getReturnType()
      );
}

Full Program Listing

package com.avaldes.tutorials;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;

public class GetPublicMethodsExample {

  public static void main(String[] args) {
    Mammal dog = new Mammal();
    Class dogClass = dog.getClass();
  
    System.out.println("\nDetails for " + dogClass.getCanonicalName());
    Method[] publicMethods = dogClass.getMethods();
    for (Method method : publicMethods) {
      int mods = method.getModifiers();
      
      System.out.println("  PUBLIC METHODS are " + method.getName() 
          + " mods=" + Modifier.toString(mods)
          + " retType=" + method.getReturnType()
          );
    }
  }
}

Full Program Listing (Mammal.java)

package com.avaldes.tutorials;

public class Mammal implements Animal {

  private boolean hair = false;
  private String name = null;
  private static int MammalCount = 0; 
  
  public Mammal() {
    incCount();
  }
  
  public String getName() {
    return name;
  }
  
  public static int getMammalCount() {
    return MammalCount;
  }


  private static void incCount() {
    MammalCount++;
  }

  public void setName(String name) {
    this.name = name;
  }

  private void privateMethod() {
    System.out.println("Private Method!!!");
  }
  
  protected void protectedMethod() {
    System.out.println("Protected Method!!!");
  }
  
  @Override
  public void eat() {
    System.out.println("Mammal is eating...");
  }

  @Override
  public void sleep() {
    System.out.println("Mammal is Sleeping...");
  }

  @Override
  public void walk() {
    System.out.println("Mammal is Walking...");
  }

  public boolean isHair() {
    return hair;
  }

  public void setHair(boolean hair) {
    this.hair = hair;
  }

  public String toString() {
    return "[name=" + getName() + ", hair=" + isHair() 
                    + ", Mammals=" + getMammalCount() + "]";
  }
}

Output

Details for com.avaldes.tutorials.Mammal
  PUBLIC METHODS are toString mods=public retType=class java.lang.String
  PUBLIC METHODS are getName mods=public retType=class java.lang.String
  PUBLIC METHODS are setName mods=public retType=void
  PUBLIC METHODS are sleep mods=public retType=void
  PUBLIC METHODS are setHair mods=public retType=void
  PUBLIC METHODS are getMammalCount mods=public static retType=int
  PUBLIC METHODS are eat mods=public retType=void
  PUBLIC METHODS are walk mods=public retType=void
  PUBLIC METHODS are isHair mods=public retType=boolean
  PUBLIC METHODS are wait mods=public final native retType=void
  PUBLIC METHODS are wait mods=public final retType=void
  PUBLIC METHODS are wait mods=public final retType=void
  PUBLIC METHODS are equals mods=public retType=boolean
  PUBLIC METHODS are hashCode mods=public native retType=int
  PUBLIC METHODS are getClass mods=public final native retType=class java.lang.Class
  PUBLIC METHODS are notify mods=public final native retType=void
  PUBLIC METHODS are notifyAll mods=public final native retType=void

Java Reflection Examples

  • Class Example
    Simple example shows you how to dynamically load and instantiate a class, get class name, package name, and superclass name.
  • Modifier Example
    Showing you how to get information about classes, fields and methods using modifier method.
  • Method Example
    Providing full details of how to get all methods, and invoke methods using reflection.

Frequently Asked Questions on Java Reflection API (FAQs)

Please Share Us on Social Media

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *