aspose file tools*
The moose likes Java in General and the fly likes Using Class.forName() with constructor arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using Class.forName() with constructor arguments" Watch "Using Class.forName() with constructor arguments" New topic
Author

Using Class.forName() with constructor arguments

Randy Johnson
Greenhorn

Joined: Apr 17, 2002
Posts: 22
Is there a way to use the Class.forName() method to instantiate an object with a constructor signature that includes arguments? I know the javadocs indicate that a no-arg constructor must be supplied, but was hoping someone came up with an ingenious way to make it work.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Class.forName() load a class; I think you're thinking of Class.newInstance() which creates an instance of that class.

But the answer to your question is absolutely "yes". You can call the "getConstructors" method of the Class object returned from Class.forName() to get a list of java.lang.reflect.Constructor objects. This class has a newInstance() method that lets you pass arguments to the constructor.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Class.forName() with constructor arguments
 
Similar Threads
how to write a new line on a file
writing methods which returns different Objects
Answer :Run time error / why?
Elegant way for creating objects depending on information from DB
Dynamic class loading