Instantiating a class read in from user input  
Author Message
Jeff Palmer





PostPosted: 11/3/2003 1:46:00 AM Top

java-programmer, Instantiating a class read in from user input I am trying to write a simple application that will instantiate a class
whose name is read in from user input. Since the name of the class is being
read in as a String, how can it be instantiated? For example, suppose I
store the name of the user supplied class in a String variable called
ClassName. I can't simple just instantiate the class with a statement such
as new ClassName(), since ClassName is a String. Any advice?


 
Jeff Palmer





PostPosted: 11/3/2003 1:47:00 AM Top

java-programmer >> Instantiating a class read in from user input I am trying to write a simple application that will instantiate a class
whose name is read in from user input. Since the name of the class is being
read in as a String, how can it be instantiated? For example, suppose I
store the name of the user supplied class in a String variable called
ClassName. I can't simple just instantiate the class with a statement such
as new ClassName(), since ClassName is a String. Any advice?


 
Jeff Palmer





PostPosted: 11/3/2003 1:49:00 AM Top

java-programmer >> Instantiating a class read in from user input I am trying to write a simple application that will instantiate a class
whose name is read in from user input. Since the name of the class is being
read in as a String, how can it be instantiated? For example, suppose I
store the name of the user supplied class in a String variable called
ClassName. I can't simple just instantiate the class with a statement such
as new ClassName(), since ClassName is a String. Any advice?


 
 
Chiron Paixos





PostPosted: 11/3/2003 3:50:00 AM Top

java-programmer >> Instantiating a class read in from user input Jeff,
you do want to read the API docs for ClassLoader:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ClassLoader.html

HTH
 
 
VisionSet





PostPosted: 11/3/2003 3:53:00 AM Top

java-programmer >> Instantiating a class read in from user input "Jeff Palmer" <email***@***.com> wrote in message
news:email***@***.com...
> I am trying to write a simple application that will instantiate a class
> whose name is read in from user input. Since the name of the class is
being
> read in as a String, how can it be instantiated? For example, suppose I
> store the name of the user supplied class in a String variable called
> ClassName. I can't simple just instantiate the class with a statement such
> as new ClassName(), since ClassName is a String. Any advice?
>

What *exactly* are you trying to achieve?

What is the *root* problem you are trying to solve?

--
Mike W



 
 
Tor Iver Wilhelmsen





PostPosted: 11/3/2003 5:49:00 AM Top

java-programmer >> Instantiating a class read in from user input "Jeff Palmer" <email***@***.com> writes:

> I am trying to write a simple application that will instantiate a class
> whose name is read in from user input. Since the name of the class is being
> read in as a String, how can it be instantiated?

Class.forName(theString)

If the class has a no-args constructor, use newInstance(), otherwise
look for a constructor using the relevant Class methods.