How to Get All Method Names from Class in Java

Get All Method Names in Java

In this program, We will show you how to get all method names from class in Java. The example program has been tested and output were shared in the same post.

1) Input POJO (Plain Old Java Object) class

package com.dineshkrish;

public class Customer {

	private int custId;
	private String custName;
	private String custAddress;
	private int custAge;

	public int getCustId() {
		return custId;
	}

	public void setCustId(int custId) {
		this.custId = custId;
	}

	public String getCustName() {
		return custName;
	}

	public void setCustName(String custName) {
		this.custName = custName;
	}

	public String getCustAddress() {
		return custAddress;
	}

	public void setCustAddress(String custAddress) {
		this.custAddress = custAddress;
	}

	public int getCustAge() {
		return custAge;
	}

	public void setCustAge(int custAge) {
		this.custAge = custAge;
	}

}

2) Retrieving Method Names from Class

package com.dineshkrish;

import java.lang.reflect.Method;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class MethodList {

	public static void main(String[] args) {
				
		try {
			
			// Loading the class
			Class<?> c = Class.forName("com.dineshkrish.Customer");
		
			// getting all method list
			Method[]  methodList = c.getMethods();
			
			if(methodList != null && methodList.length > 0) {
				
				System.out.println("-------------------------------------");
				
				System.out.println("Printing Method Names");
				
				System.out.println("-------------------------------------");
				
				for (int i = 0; i < methodList.length; i++) {
					
					System.out.println(methodList[i].getName());
					
				}
				
				System.out.println("-------------------------------------");
				
				System.out.println("Printing Complete Method Signature");
				
				System.out.println("-------------------------------------");
				
				for (int i = 0; i < methodList.length; i++) {
					
					System.out.println(methodList[i]);
					
				}
				
			}
			
			
			
		} catch (ClassNotFoundException e) {
			
			System.out.println(e.getMessage());
			e.printStackTrace();
		} 		
	}
	
}

Output

————————————-
Printing Method Names
————————————-
setCustId
getCustName
setCustName
getCustAge
setCustAge
getCustId
getCustAddress
setCustAddress
getClass
hashCode
equals
toString
notify
notifyAll
wait
wait
wait
————————————-
Printing Complete Method Signature
————————————-
public void com.dineshkrish.Customer.setCustId(int)
public java.lang.String com.dineshkrish.Customer.getCustName()
public void com.dineshkrish.Customer.setCustName(java.lang.String)
public int com.dineshkrish.Customer.getCustAge()
public void com.dineshkrish.Customer.setCustAge(int)
public int com.dineshkrish.Customer.getCustId()
public java.lang.String com.dineshkrish.Customer.getCustAddress()
public void com.dineshkrish.Customer.setCustAddress(java.lang.String)
public final native java.lang.Class java.lang.Object.getClass()
public native int java.lang.Object.hashCode()
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
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 final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException

References

1. Java Method Reflection API