Introduction to OOP | Chapter 25: Reflection and Introspection : | next | previous | audio | real | text |
Class sc = String.class; Class [ ] paramTypes = new Class[1]; paramTypes[0] = sc; try { Method mt = sc.getMethod("concat", paramTypes); Object mtArgs [ ] = { "xyz" }; Object result = mt.invoke("abc", mtArgs); System.out.println("result is " + result); } catch (Exception e) { System.out.println("Exception " + e); }Here we dynamically look up a method, based both on name and type signature, then create an array of arguments, then execute the method.