Results 1 to 4 of 4
- 12-02-2010, 03:17 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 5
- Rep Power
- 0
How to Instance and reference a class by its name????
Hi guys. I got a problem here. How can I get an instance of a specific class having the class name and casting it to the correct class type. I know that using the Class.forName("className").newInstance () will do the trick.
But the problem is that the newInstance () method returns an Object and I need to have a reference to the specific class. The className is read from a database and I need to execute a method from that class.
public void execute (String className, ArrayList params) {
Class.forName (className).newInstance ();
........
}
now lets suposse className = "package.Validator"
and I need to execute Validator.validate(params) but as I get an Object I cant execute the validate method.
I cannot do a cast like (Validator)Class.forName (className).newInstance ();
becausse the classname is dinamyc.
Thanks in advance.:confused:
- 12-02-2010, 07:11 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
The className is read from a database and I need to execute a method from that class.
Have a look at Invoking Methods in Oracle's Tutorial.
- 12-08-2010, 04:24 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 5
- Rep Power
- 0
Solved
Thanks a lot. It did the trick. I was not sure about it becausse I had to use a third party service that returns an Object not a Class, but everything worked with those invoking methods!
- 12-08-2010, 10:28 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Similar Threads
-
ISSUE: Array as class constructor will not copy elements, only reference
By Lucificate in forum New To JavaReplies: 16Last Post: 07-08-2010, 09:13 PM -
deploy existing web project[class reference problem]
By runfast in forum EclipseReplies: 1Last Post: 10-13-2009, 10:38 AM -
Static and non staic reference, Synchronized and unsynchronized, abstarct class,
By Basit56 in forum New To JavaReplies: 3Last Post: 08-17-2009, 10:59 PM -
DefaultListModel being updated in another class (by reference?)
By rickyoswald in forum Advanced JavaReplies: 3Last Post: 04-24-2009, 06:28 PM -
Passing Class Reference to method
By nekt in forum Advanced JavaReplies: 5Last Post: 03-26-2009, 05:08 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks