19

I am storing a list of classes through (Classname.class) and would like to instantiate one? Is this possible?

newInstance seems to the method I am after but it doesn't support a constructor?

6 Answers 6

45

You can use Class.getConstructors (or Class.getConstructor) to get a list of available constructors, and invoke any of them with Constructor.newInstance, which does accept parameters.

Sign up to request clarification or add additional context in comments.

Comments

11

Just to add one point I see missing:

You can invoke newInstance directly on the Class object if it has a public null constructor. (Null constructor is the constructor with no arguments.)

Otherwise, you can find constructors via Class.getConstructors() as others have said.

1 Comment

The Class.newInstance method is now deprecated.
8

The Java tutorial on reflection covers this well. But yeah, basically Class.getConstructors, then Constructor.newInstance is where it's at.

Comments

6

Java is designed so you can never "trick" it as long as you use the java.lang/java. classes or other standard libraries. One of the most important things of OOP is that objects should be in a defined state, thus you can be safe that the constructor is always run. Even if you're using some strange-looking reflection libraries to get your work done.

So, using Class.forName("me.Test").newInstance(); (or similar) will under-the-hood invoke the Test() constructor for you.

If you want to invoke another constructor the code is something like:

Test test = (Test)Class.forName("Test").getConstructor(String.class).newInstance("Hello World");

Here the getConstructor asks what the constructor looks like (it wants a string) and then you call it with a string.

Comments

3

You cannot construct new classes this way.

If you have the name of a class you can use Class.forName(className) to load/reference a class.

If you have the byte code for a class you want to create you can have a class loader load the byte code and give you the class. This is likely to be more advanced than you intended.

2 Comments

He's asking about how to instantiate a new instance of the class, not create the Class object (nor how to get the byte code). This should not be marked as the correct answer. Keep reading to andri's answer below.
@RyanShillington This answers the original question. The question was edited to be a different question which is what andri answered.
0

If you have a list of Class objects obtained through class literals, you might as well statically reference the constructors rather than slipping into reflection evilness.

1 Comment

Maybe the instantiation is being done in a service program that will be linked together with several other programs that have not even been created yet. They will call the service program, each with a different list containing Class variables for classes that did not exist when the service program was written.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.