Mega Code Archive

 
Categories / Java Tutorial / Reflection
 

Instantiate unknown class at runtime and call the objects methods

import java.lang.reflect.Method; public class Main {   public static void main(String[] args) throws Exception {     Class myclass = Class.forName("java.lang.String");     Method[] methods = myclass.getMethods();     //Object object = myclass.newInstance();     for (int i = 0; i < methods.length; i++) {       System.out.println(methods[i].getName());       //System.out.println(methods[i].invoke(object));     }   } } /* hashCode compareTo indexOf equals toString charAt codePointAt codePointBefore codePointCount compareToIgnoreCase concat contains contentEquals copyValueOf endsWith equalsIgnoreCase format getBytes getChars intern isEmpty lastIndexOf length matches offsetByCodePoints regionMatches replace replaceAll replaceFirst split startsWith subSequence substring toCharArray toLowerCase toUpperCase trim valueOf getClass wait notify notifyAll */