Mega Code Archive

 
Categories / Java Tutorial / Reflection
 

Invoke methods of an object using reflection

import java.lang.reflect.Method; public class Main {   public static void main(String[] args) throws Exception {     Class computerClass = MyClass.class;     Method[] methods = computerClass.getDeclaredMethods();     MyClass computer = new MyClass();     for (Method method : methods) {       Object result = method.invoke(computer, new Object[0]);       System.out.println(method.getName() + ": " + result);     }   } } class MyClass {   private String type = "type";   public String getType() {     return type;   } } //getType: type