November 2006
Intermediate to advanced
224 pages
3h 29m
English
Baseball bbObj = new Baseball(); Class c = Baseball.class; Class[] paramTypes = new Class[] {int.class, int.class}; Method calcMeth = c.getMethod("calcBatAvg", paramTypes); Object[] args = new Object[] {new Integer(30), new Integer(100)}; Float result = (Float) calcMeth.invoke(bbObj, args); |
The reflection API allows you to dynamically invoke methods even if the method name that you want to invoke is not known at compile time. In order to invoke a method, you must first get a Method object for the method that you want to invoke. See the phrase “Discovering Method Information” earlier in this chapter for more information about getting Method objects from a class.
In this phrase, we are trying to invoke a method that calculates ...
Read now
Unlock full access