I might have been mis-understood, I want to create objects and specify which class the object is by using a string.
Asked
Active
Viewed 84 times
-2
-
1What do you mean by "class calling"? – Artem Chilin May 04 '12 at 22:31
-
1What do you mean by "call a class"? – Oliver Charlesworth May 04 '12 at 22:31
-
3I think the OP needs to read up on [reflection](http://java.sun.com/developer/technicalArticles/ALT/Reflection/). – mpontillo May 04 '12 at 22:31
-
1*"I'd like to know if there's a way to call a class by specifying it's name"* While vital for some uses, reflection is often the 'right answer to the wrong question'. What are you actually trying to ***achieve*** by loading classes by their name? – Andrew Thompson May 04 '12 at 22:40
1 Answers
5
Yes, you can use reflection for that.
To get a class, use
// if your class is in package com.foo and is named Bar
Class<?> klass = Class.forName("com.foo.Bar");
Read the tutorial for more details.

Guillaume Polet
- 47,259
- 4
- 83
- 117