Loading the java class dynamically

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
jabbaaAsked:
Who is Participating?
 
WelkinMazeCommented:
Object obj = Class.forName(arg).newInstance();
And the passed argument has to be with the full packages path before the class name.
0
 
WelkinMazeCommented:
You can use for example Class.forName( classNameString ).newInstance();
0
 
gauravkrtomarCommented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
jabbaaAuthor Commented:
But how about if my constructor have parameter?
e.g . new Class1(arg1, arg2)

Thanks
0
 
chandru_inCommented:
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
 
gauravkrtomarCommented:
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
 
WelkinMazeCommented:
You can do it this way:

Class cls = Class.forName(arg);
Object obj = cls.getConstructor(new Class[]{cls}).newInstance(new Object[]{arg1, arg2});
0
 
gauravkrtomarCommented:
Yeah that can also be one of the better ways to do that :)
0
 
WebstormCommented:

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
 
Jim CakalicSenior Developer/ArchitectCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.