10 Replies - 5384 Views - Last Post: 23 May 2011 - 12:46 AM Rate Topic: -----

#1 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Instantiation of a class whose only name is known

Posted 22 May 2011 - 12:37 AM

Hi,
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()
Is This A Good Question/Topic? 0
  • +

Replies To: Instantiation of a class whose only name is known

#2 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: Instantiation of a class whose only name is known

Posted 22 May 2011 - 01:02 AM

I think you are asking if you can instantiate an object in your main method using the class' string variable as a parameter?
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

Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Instantiation of a class whose only name is known

Posted 22 May 2011 - 03:30 AM

Vortex I thing the OP original question is:

String str = "Cat";

Object o = new whateverIsInStr();

Class class = Class.forName("Cat");
Was This Post Helpful? 3
  • +
  • -

#4 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: Instantiation of a class whose only name is known

Posted 22 May 2011 - 03:36 AM

Ye, you are most likely correct. Lol :P
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.
Was This Post Helpful? 2
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1277
  • View blog
  • Posts: 2,846
  • Joined: 05-April 11

Re: Instantiation of a class whose only name is known

Posted 22 May 2011 - 04:21 AM

Like pbl said:
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

Was This Post Helpful? 4
  • +
  • -

#6 alpha_x  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 55
  • Joined: 03-May 11

Re: Instantiation of a class whose only name is known

Posted 22 May 2011 - 04:29 AM

Both solutions work but I recommend going with pbl's solution.
Was This Post Helpful? 1
  • +
  • -

#7 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: Instantiation of a class whose only name is known

Posted 22 May 2011 - 04:31 AM

Use pbl's solution, You could even implement a function like so if you think you will be instantiating classes like this often:
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

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Instantiation of a class whose only name is known

Posted 22 May 2011 - 04:50 AM

View Postv0rtex, on 22 May 2011 - 07:31 AM, said:

Wow, yes thats a hell of a lot easier, Either way does the trick though :)

No :) Your solution implies that you know that the String contains "Cat" to create your handle.
Was This Post Helpful? 2
  • +
  • -

#9 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: Instantiation of a class whose only name is known

Posted 22 May 2011 - 04:53 AM

Ah ok, thats actually true, didn't see that. Just did some pseudo code quickly, never actually ran it. :whatsthat:

This post has been edited by v0rtex: 22 May 2011 - 04:55 AM

Was This Post Helpful? 0
  • +
  • -

#10 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: Instantiation of a class whose only name is known

Posted 23 May 2011 - 12:42 AM

I have to study more about the class Class. This is helpful.
Was This Post Helpful? 1
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Instantiation of a class whose only name is known

Posted 23 May 2011 - 12:46 AM

View Postsakshamkum, on 23 May 2011 - 03:42 AM, said:

I have to study more about the class Class. This is helpful.

You are in business my friend. After that you could write a tutorial about it. :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1