Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Loading the java class dynamically

Posted on 2006-04-26
10
Medium Priority
?
274 Views
Last Modified: 2011-09-20
Let say I have serveral java class:

Class1, Class2, Class3


Then I have a command line program which allow user to input the paramter
java Program Class1

which will then run the following
new Class1()
if the parameter is Class2
new Class2()

How can I write the code such that I can create the class dynamically?
I don't want using the if else case...like..
if(arg.equals("Class1"))
   new Class1();
else if(arg.equals("Class2"))
   new Class2();
else if(arg.equals("Class3"))
   new Class3();



Thanks
0
Comment
Question by:jabbaa
10 Comments
 
LVL 11

Expert Comment

by:WelkinMaze
ID: 16550527
You can use for example Class.forName( classNameString ).newInstance();
0
 
LVL 11

Accepted Solution

by:
WelkinMaze earned 200 total points
ID: 16550535
Object obj = Class.forName(arg).newInstance();
And the passed argument has to be with the full packages path before the class name.
0
 
LVL 2

Expert Comment

by:gauravkrtomar
ID: 16550698
Just do make sure that you have a default constructer for the Class, default constructur i.e. a no argument constructer, otherwise u'll get an error.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:jabbaa
ID: 16550756
But how about if my constructor have parameter?
e.g . new Class1(arg1, arg2)

Thanks
0
 
LVL 3

Expert Comment

by:chandru_in
ID: 16550777
It is not possible to call parameterized constructor from Class.forName().  However, you can write a dummy no-arg contructor within the class which would call the parameterized constructor with some default parameters.
0
 
LVL 2

Expert Comment

by:gauravkrtomar
ID: 16550807
or u have to initialise them in your switch statement.

somthing like:-
 Object obj = Class.forName(arg).newInstance();
((CustomObject)obj).initialise(arg1,arg2);

where CustomObject can be the interface or the class u have created.
0
 
LVL 11

Expert Comment

by:WelkinMaze
ID: 16550827
You can do it this way:

Class cls = Class.forName(arg);
Object obj = cls.getConstructor(new Class[]{cls}).newInstance(new Object[]{arg1, arg2});
0
 
LVL 2

Expert Comment

by:gauravkrtomar
ID: 16550858
Yeah that can also be one of the better ways to do that :)
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16552898

Class cls = Class.forName(arg);

Object obj = cls.getConstructor(new Class[]{
           // here put the argument classes, not cls
           String.class,String.class // if arg1 and arg2 are String
         }).newInstance(new Object[]{arg1, arg2});

If you want to call a contructor with int parameters, use Integer
int -> Integer
long -> Long
double -> Double
...
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 16554106
You also might have a look at the commons.beanutils.ConstructorUtils class. It abstracts away a lot of the noise:
http://jakarta.apache.org/commons/beanutils/api/index.html

Jim
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month21 days, 6 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question