Solved

Can type of instantiated object depend DIRECTLY on a String?

Posted on 2007-03-28
7
216 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 150 total points
Comment Utility
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
Comment Utility
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
Comment Utility
... and you will be able call that method the following way:

// obtain an object of Bob type
Object myRuntimeObject = createObject("my.persons.Bob");
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:BrianMc1958
Comment Utility
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
Comment Utility
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:mayankeagle
mayankeagle earned 150 total points
Comment Utility
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
Comment Utility
Well, you folks have given me exactly what I asked for.  Thanks again...
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

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…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now