Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Calling class methods for annoymous class object?



You can get the specific method you want by passing the name and parameters to the getMethod method of the Class class. Then you can call the invoke method on the Method, passing in the instance of the object you want to invoke the method on and the arguments. For example:

Argument1 argument1 = new Argument1();
Argument2 argument2 = new Argument2();
Argument3 argument3 = new Argument3();
Class class = Class.forName("my.class.name");
Method method = class.getMethod("methodName", new Class[] {Argument1.class, Argument2.class, Argument3.class});
Object result = method.invoke(class, new Object[] {argument1, argument2, argument3});


- Erik Mattheis
  email@hidden


On Jul 16, 2006, at 8:52 PM, Saad Mahamood wrote:

Hello,

I'm writing a file parser that needs to the results of it parsing into
a particular class. In the name of good code re-use in the future I
didn't want this particular class name (AddressItem.class in this
case) to be hard coded. Instead I pass a String with the name of the
class to the constructor and do something of the following order:


String className = "AddressItem.class"; Class classObj = Class.forName(className); Method[] mList = classObj.getMethods(); //Instantiation: Object item = classObj.newInstance();



So I have new instance object and a Method ArrayList which contains
the names of the methods in the Class. But what I don't understand is
how to call a particular method using it's name?

Many Thanks,

Saad Mahamood.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden

_______________________________________________ Do not post admin requests to the list. They will be ignored. Java-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden
References: 
 >Calling class methods for annoymous class object? (From: "Saad Mahamood" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.