Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create new object from a string in Java

Tags:

java

Is there a way to create a new class from a String variable in Java?

String className = "Class1";
//pseudocode follows
Object xyz = new className(param1, param2);

Also, if possible does the resulting object have to be of type Object?

There may be a better way, but I want to be able to retrieve values from an XML file, then instantiate the classes named after those strings. Each of these classes implement the same interface and are derived from the same parent class, so I would then be able to call a particular method in that class.

like image 467
jW. Avatar asked Aug 12 '09 21:08

jW.


People also ask

Can we convert String to object in Java?

We can also convert the string to an object using the Class. forName() method. Parameter: This method accepts the parameter className which is the Class for which its instance is required.

Does to String create a new object?

String class posses a method name length() , while arrays have an attribute naming length. In java, string objects are immutable. Immutable simply means unmodifiable or unchangeable. Once string object is created its data or state can't be changed but a new string object is created.

How do you create an object object in Java?

There are three steps when creating an object from a class − Declaration − A variable declaration with a variable name with an object type. Instantiation − The 'new' keyword is used to create the object. Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.


2 Answers

This is what you want to do:

String className = "Class1";
Object xyz = Class.forName(className).newInstance();

Note that the newInstance method does not allow a parametrized constructor to be used. (See Class.newInstance documentation)

If you do need to use a parametrized constructor, this is what you need to do:

import java.lang.reflect.*;

Param1Type param1;
Param2Type param2;
String className = "Class1";
Class cl = Class.forName(className);
Constructor con = cl.getConstructor(Param1Type.class, Param2Type.class);
Object xyz = con.newInstance(param1, param2);

See Constructor.newInstance documentation

like image 84
Dawie Strauss Avatar answered Oct 13 '22 06:10

Dawie Strauss


Yes, you can load a class on your classpath given the String name using reflection, using Class.forName(name), grabbing the constructor and invoking it. I'll do you an example.

Consider I have a class:

com.crossedstreams.thingy.Foo

Which has a constructor with signature:

Foo(String a, String b);

I would instantiate the class based on these two facts as follows:

// Load the Class. Must use fully qualified name here!
Class clazz = Class.forName("com.crossedstreams.thingy.Foo");

// I need an array as follows to describe the signature
Class[] parameters = new Class[] {String.class, String.class};

// Now I can get a reference to the right constructor
Constructor constructor = clazz.getConstructor(parameters);

// And I can use that Constructor to instantiate the class
Object o = constructor.newInstance(new Object[] {"one", "two"});

// To prove it's really there...
System.out.println(o);

Output:

com.crossedstreams.thingy.Foo@20cf2c80

There's plenty of resources out there which go into more detail about this, and you should be aware that you're introducing a dependency that the compiler can't check for you - if you misspell the class name or anything, it will fail at runtime. Also, there's quite a few different types of Exception that might be throws during this process. It's a very powerful technique though.

like image 14
brabster Avatar answered Oct 13 '22 06:10

brabster