Solved

Can type of instantiated object depend DIRECTLY on a String?

Posted on 2007-03-28
7
250 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 150 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 200 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 150 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 53
What's wrong with this code? 4 36
Eclipse Java import and method not resolved 4 81
activeMQ Queue Messages Enqueued and Messages Dequeued 1 38
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

733 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