Discovering Method Information
Class c = someObject.getClass(); Method[] methods = c.getMethods(); for (int i = 0; i < methods.length; i++) { String methodName = methods[i].getName(); System.out.println("Name: " + methodName); String returnType = methods[i].getReturnType().getName(); System.out.println("Return Type: " + returnType); Class[] paramTypes = methods[i].getParameterTypes(); System.out.print("Parameter Types:"); for (int k = 0; k < paramTypes.length; k ++) { String paramTypeStr = paramTypes[k].getName(); System.out.print(" " + paramTypeStr); } System.out.println(); } |
You can get information about a class’s public methods by calling the getMethods() method on a Class object. This method returns an array of Method objects. Using the ...
Get Java™ Phrasebook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.