m3developer2
asked on
How do I declare and instantiate a new class object, but with a String name reference instead?
I have a 'controller' class. From my main() in the 'entry' class, I call the controller class "ThreadInit" to declare and instantiate one of 4 class objects, so I need to make this generic, accepting a String classname to instantiate, to then start new threads...
Here's my controller class...
import java.lang.Thread;
public class ThreadInit implements Runnable{
private Thread t;
private String threadName;
private DButil obj = null; //gets the database object
public ThreadInit(String className) {
threadName = className;
}
public void start() {
if (t == null){
t = new Thread(this, threadName);
t.start();
}
}
@Override
public void run() {
process();
}
private void process() {
this.obj = new DButil();
if (obj.getConn() != null) {
//MPhead mphead1 = new MPhead(obj);
// It is here, I want to make this more generic to handle any classname and instantiate // ... it, to further start a thread
Class classObj = new Class.forName(threadName);
//logger.appendLog(mphead1 .getLog(). toString() );
} else {
logger.appendLog(" No Connection");
}
}
}
What is the best method to do this? Thank you!
Here's my controller class...
import java.lang.Thread;
public class ThreadInit implements Runnable{
private Thread t;
private String threadName;
private DButil obj = null; //gets the database object
public ThreadInit(String className) {
threadName = className;
}
public void start() {
if (t == null){
t = new Thread(this, threadName);
t.start();
}
}
@Override
public void run() {
process();
}
private void process() {
this.obj = new DButil();
if (obj.getConn() != null) {
//MPhead mphead1 = new MPhead(obj);
// It is here, I want to make this more generic to handle any classname and instantiate // ... it, to further start a thread
Class classObj = new Class.forName(threadName);
//logger.appendLog(mphead1
} else {
logger.appendLog(" No Connection");
}
}
}
What is the best method to do this? Thank you!
Rather than using reflection, you can also use a Factory pattern, since you only have a few possible different types
ASKER
Can you give me a code sample using 'threadName' as the class name reference to instantiate?
I think there's some confusion here.
The second parameter can only ever be a String anyway, so it don't see how that applies to a 'new class object' ..?
t = new Thread(this, threadName);
The second parameter can only ever be a String anyway, so it don't see how that applies to a 'new class object' ..?
ASKER
Let me try a different example. I have a controller class named 'start', with the main() method, and some 'thread' classes. The controller class should : 1) Iterate an String array of class Names, 2) Instantiate each 'thread' class as an object, 3) Invoke it's run() method. The objective is that each 'thread' class is a database call, since queries are unknown runtimes, each will be in it's own thread.
Can I get an example of code to dynamically declare/instantiate the class names in 'start'
Here are the classes...
Can I get an example of code to dynamically declare/instantiate the class names in 'start'
Here are the classes...
Controller:
package model;
public class start {
public static void main(String[] args) {
String[] classNames = new String[]{"model.Thread1","model.Thread2"};
startThreads(classNames);
}
public static void startThreads(String[] classNames) {
for (int i = 0; i < classNames.length; i++) {
try {
// Instantiate an object using the String className at index i
Class cl = Class.forName(classNames[i].toString());
// invoke the run() method of the current instantiated class here, starting it as a thread...
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
Thread classes
public class Thread1 implements Runnable{
public Thread1() {
}
@Override
public void run() {
// GO run a query and do something with it, take all the time it requires...
System.out.println("Starting Thread1...");
}
}
public class Thread2 implements Runnable{
@Override
public void run() {
System.out.println("Starting thread2");
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you CEHJ, although, I can't seem to find the jar for the foreign classes at that link....
I signed up, just dont see the download...
Thank you!
I signed up, just dont see the download...
Thank you!
ASKER
Nevermind!, I just found the link in the word 'Help'...
Thanks, I'll try this, this site is great for other challenges...
Thanks, I'll try this, this site is great for other challenges...
:)
I think i need to make it more obvious
I signed up, just dont see the download...
I think i need to make it more obvious