Solved

Can type of instantiated object depend DIRECTLY on a String?

Posted on 2007-03-28
7
257 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 covers a step-by-step guide to install VisualVM launcher in eclipse.

717 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