?
Solved

Can type of instantiated object depend DIRECTLY on a String?

Posted on 2007-03-28
7
Medium Priority
?
271 Views
Last Modified: 2011-10-03
Dear Experts,

I know in advance that my project contains classes named Bob and Alice.

Is is possible to construct a factory method such that I pass in a literal String "Bob" or "Alice" and have it return an Object of type Bob or Alice?

So it would be:

Object someObject = getObjectBasedOnName("Bob");

and someObject would be of type Bob.

But the method getObjectBasedOnName CANNOT directly know what classes it's supposed to create.  It can't have a statement that says:

if (parameter.equals("Bob"))
  return new Bob();
else (parameter.equals("Alice"))
  return new Alice();

Instead, it has to magically make a new object based DIRECTLY on the name, like:

return new <<someMagicalStringToObjectTechniqueGoesHere...>>();

(BTW, I know the answer is probably, No, stupid.  Thought I would ask...)

--BrianMc1958
0
Comment
Question by:BrianMc1958
7 Comments
 
LVL 3

Assisted Solution

by:VoxSis
VoxSis earned 600 total points
ID: 18808319
Hi
I don't know to answer directly to your question but you should read about ClassLoader at
http://java.sun.com/j2se/1.3/docs/api/java/lang/ClassLoader.html
and see if it can be in any help.

Regards
0
 
LVL 11

Accepted Solution

by:
elfe69 earned 800 total points
ID: 18808373
If you have the fully qualified name of a class, including the package name, you can create a new instance of the class with Class.forName("myClassName").newInstance(), for example if your Bob and Alice classes are in a package named my.persons, you could write a method like this:

public final static Object createObject(String classname) throws Exception {
  return Class.forName(classname).newInstance();
}
0
 
LVL 11

Expert Comment

by:elfe69
ID: 18808394
... and you will be able call that method the following way:

// obtain an object of Bob type
Object myRuntimeObject = createObject("my.persons.Bob");
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:BrianMc1958
ID: 18808489
To elfe69:
>>If you have the fully qualified name of a class, including the package name
Does "fully qualified" mean "location on the disk drive"?  Like "C:\MyDir\my.persons.Bob"?
0
 
LVL 11

Expert Comment

by:elfe69
ID: 18808546
No, it means the package name of your class, like "experts.persons" in the following source file:

package experts.persons;

public class Bob {
      public String getName() {
            return "Hello, this is Bob";
      }
}

You could create an object of the Bob class this way:

Object myRuntimeObject = createObject("experts.persons.Bob");
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 600 total points
ID: 18808938
Yes, you need to use reflection for this. Have a look at the explanation I'd given here for a similar scenario (that was related to data-stores):

http://www.experts-exchange.com/Programming/Languages/Java/Q_21666226.html#15495977
0
 

Author Comment

by:BrianMc1958
ID: 18809917
Well, you folks have given me exactly what I asked for.  Thanks again...
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question