Alan Blass wrote:Errrr....so line 4 should read:
?
No. If you could reference com.mycom.MyClass you wouldn't be using reflection, would you? You would just create the object normally. The problem is the instance (myObject) doesn't hold references to the Methods, the class does. So you need to get a reference to the Class object. You try that using
myObject.class but that won't work, because Object does not have a publicly accessible member called class. You could get the class Object using
myObject.getClass() because Object does have an accessible getClass() method. But you don't need to: you already have the Class object
Class c.
So use:
And if the method is not a static method then line 5 will fail. You need to pass it two things (based on the method signature): 1: the instance
myObject, on which the method will be executed and 2: a
String parameter.