• Post Reply Bookmark Topic Watch Topic
  • New Topic

Instantiate an object of a class using a variable?  RSS feed

 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question about creating a new instance of a class using a variable.  I do not have the exact code for the method written yet.  At the moment I am planning the project.  The project begins with a base class and child classes extend the base.

Is it possible to create an instance of one of the child classes if the class name is defined using a variable?

I hope this question makes sense.
 
Fred Kleinschmidt
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you should look into the factory design pattern. there are many tutorials on this - just google "java factory design"
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to create an instance of one of the child classes if the class name is defined using a variable?

Yes, with reflection.  I assume you mean there is a String that holds the name of the class.  For example, you want to create an instance of MyClass using the String "MyClass".
The reflection package has methods for calling a class's constructor given the name in  a String.  See the classes in the java.lang.reflect package.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes, with reflection.  I assume you mean there is a String that holds the name of the class.  For example, you want to create an instance of MyClass using the String "MyClass".
The reflection package has methods for calling a class's constructor given the name in  a String.  See the classes in the java.lang.reflect package.

Thank you.  I will look into this.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My gut feeling is that reflection is not the right solution in this case (I could be wrong).  Tell us the problem you're trying to solve and perhaps we can suggest a better way.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After doing some surfing, reflection will do what I wanted to do, but I think I was making the program more difficult than it needs to be.

Thanks for the help.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use reflection to instantiate classes, but it can be awkward. There are several checked Exceptions which the methods used declare, and the class instantiated needs an accessible no‑arguments constructor.
Foo f = Class.forName("packagename.Foo").newInstance();
Not sure which is the right answer, but I think Fred Kleinschmidt's suggestion about factory methods is probably the best.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Peterson wrote:I think I was making the program more difficult than it needs to be.

Everybody has a tendency to do that. As I was telling someone recently, you tend to let your imagination run wild with lots of crazy elaborate designs if you just do thought experiments. You have to write code, look at it and recognize that you're probably over engineering things. Unit tests can help curb your enthusiasm. Complicated, over engineered designs tend to be difficult to test so you're forced to find simpler, more testable solutions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!