10 Replies - 3536 Views - Last Post: 23 May 2011 - 12:46 AM
#1
Instantiation of a class whose only name is known
Posted 22 May 2011 - 12:37 AM
I had a project where i needed to know something.
We have a class which stores the name of another compiled class in a String variable. Can we create the Object of the class whose name is stored in the main() method of class which stores the main()
Replies To: Instantiation of a class whose only name is known
#2
Re: Instantiation of a class whose only name is known
Posted 22 May 2011 - 01:02 AM
Here could be an example of your class:
public class Cat {
public String className;
Cat () { }
public void setClassName(String name) {
className = name;
}
public String getClassName() {
return className;
}
}
And here could be an example of the class you which to instantiate an object of using the className from class cat:
public class Kittens {
Kittens() { }
Kittens(String name) {
System.out.println(name + " was instantiated!");
}
}
And then you would obviously need to setClassName(String name) and getClassName() for your class, an example of your main method could be like so:
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Cat myCat = new Cat();
myCat.setClassName("cat");
Kittens cat = new Kittens(myCat.getClassName());
}
}
This is merely an example of how you could go about achieving this, I recommend adding a lot more functionality to it and I doubt that my basic template will suit your needs 100%, its merely a way for me to show you that yes what you asked is possible. If you have any more questions, ask here. If this is not what you were looking for then sorry and please rephrase your question. I hope this helps,
v0rtex
This post has been edited by v0rtex: 22 May 2011 - 01:18 AM
#4
Re: Instantiation of a class whose only name is known
Posted 22 May 2011 - 03:36 AM
If that is the case, then simply set the name of the class in a String like so:
String className = "Cat";
In order to instantiate objects using a String, I suggest simply checking the String to see if it matches a class name and if so then creating an object like so:
Cat handle = new Cat();
handle.setClassName("Kittens");
String className = handle.getClassName();
if (className.equals("Kittens")) {
Kittens smallCat = new Kittens();
//do something with object class
}
else if (className.equals("Cat")) {
Cat bigCat = new Cat();
//do something with object class
}
I'm sure there are other ways to do this but this seems practical enough for the situation.
#5
Re: Instantiation of a class whose only name is known
Posted 22 May 2011 - 04:21 AM
Class class = Class.forName("the name");
class.newInstance();
or if the class havent been loaded yet:
Class class = ClassLoader.getSystemClassLoader().loadClass("the name");
class.newInstance();
This post has been edited by CasiOo: 22 May 2011 - 04:22 AM
#7
Re: Instantiation of a class whose only name is known
Posted 22 May 2011 - 04:31 AM
public static void createClass(String name) {
Class class = Class.forName(name);
class.newInstance(); }
Remember if the class is needed elsewhere, declare it public
This post has been edited by v0rtex: 22 May 2011 - 04:53 AM
#8
Re: Instantiation of a class whose only name is known
Posted 22 May 2011 - 04:50 AM
#9
Re: Instantiation of a class whose only name is known
Posted 22 May 2011 - 04:53 AM
This post has been edited by v0rtex: 22 May 2011 - 04:55 AM
#10
Re: Instantiation of a class whose only name is known
Posted 23 May 2011 - 12:42 AM
#11
Re: Instantiation of a class whose only name is known
Posted 23 May 2011 - 12:46 AM
|
|

New Topic/Question
Reply




MultiQuote





|