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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

WelkinMazeCommented:
You can use for example Class.forName( classNameString ).newInstance();
WelkinMazeCommented:
Object obj = Class.forName(arg).newInstance();
And the passed argument has to be with the full packages path before the class name.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

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

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

Class cls = Class.forName(arg);
Object obj = cls.getConstructor(new Class[]{cls}).newInstance(new Object[]{arg1, arg2});
gauravkrtomarCommented:
Yeah that can also be one of the better ways to do that :)
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
...
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.