Print/list public methods/functions of class in java (example)

List public methods of class in java (reflections):

  • Given a class in java containing public,private & protected methods.
  • Get/list/print public methods of class using class Class.
    • We will use getMethods() of Class to retrieve public methods.
Method Name Description
Method[] getMethods() Returns an array containing Method objects reflecting all the public member methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces.

1. Print/list public methods of class in java (example)

package org.learn.classes;

import java.lang.reflect.Method;
class University {
    private String name;
    private String location;
    private float area;
    private int NumberOfDepartments;

    private University() {
    }
    public University(String name, String location, float area, int numberOfDepartments) {
        this.name = name;
        this.location = location;
        this.area = area;
        NumberOfDepartments = numberOfDepartments;
    }
    private String getName() {
        return name;
    }
    private void setName(String name) {
        this.name = name;
    }
    protected String getLocation() {
        return location;
    }
    protected void setLocation(String location) {
        this.location = location;
    }
    public float getArea() {
        return area;
    }
    protected void setArea(float area) {
        this.area = area;
    }
    public int getNumberOfDepartments() {
        return NumberOfDepartments;
    }

    public void setNumberOfDepartments(int numberOfDepartments) {
        NumberOfDepartments = numberOfDepartments;
    }
}
public class DemoListPublicMethodsOfClass {
    public static void main(String[] args) {
        Method[] methods = University.class.getMethods();
        int nMethod = 1;
        System.out.println("1. List of all public methods in a University class");
        for (Method method : methods) {
            System.out.printf("%d. %s",++nMethod,method);
            System.out.println();
        }
        System.out.printf("%d. End - all public methods in a University class",++nMethod);
    }
}

2. Print/list public methods/functions of class in java

1. List of all public methods in a University class
2. public int org.learn.classes.University.getNumberOfDepartments()
3. public void org.learn.classes.University.setNumberOfDepartments(int)
4. public float org.learn.classes.University.getArea()
5. public final void java.lang.Object.wait() throws java.lang.InterruptedException
6. public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
7. public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
8. public boolean java.lang.Object.equals(java.lang.Object)
9. public java.lang.String java.lang.Object.toString()
10. public native int java.lang.Object.hashCode()
11. public final native java.lang.Class java.lang.Object.getClass()
12. public final native void java.lang.Object.notify()
13. public final native void java.lang.Object.notifyAll()
14. End - all public methods in a University class
Scroll to Top