File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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
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: 24184
    
  34

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