?
Solved

Can type of instantiated object depend DIRECTLY on a String?

Posted on 2007-03-28
7
Medium Priority
?
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month14 days, 12 hours left to enroll

771 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