O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Invoking Methods

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required