-3

How to create an object from a class, but the object name should be variable and taken from a given string.

String objName = "customerA";
objName = new Customer("A");

Then I would like to use the object like (e.g.)

Object obj = getObjByName("customerA"); // is it possible to retrieve an object by name?
obj..getName();  //retrieve name of the customer
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
Chris
  • 1
  • 2
  • 2
    State your actual task. This example does not make any sense. – PM 77-1 May 09 '18 at 16:20
  • 1
    Use a Map to store and retrieve your objects. Your first statement should be: `Customer c = new Customer(objName)` – RubioRic May 09 '18 at 16:22
  • perhaps you wish to create a Customer Factory class that returns a different Customer object based on a given string? – RAZ_Muh_Taz May 09 '18 at 16:25
  • If you truly wish to go down the path of creating objects based on their qualified name, then you'll want to look into [Java Reflection](http://www.oracle.com/technetwork/articles/java/javareflection-1536171.html). See [this](https://stackoverflow.com/a/6094602/1601729) as an example. But note that this is generally regarded as bad practice unless you have no alternative option. It is most likely that there is a better way to do what you want. – Ironcache May 09 '18 at 16:53
  • Pursuing that further, why do you want to create an object from its name? – Ironcache May 09 '18 at 16:57
  • Your first code block makes no sense. You're creating a string variable and setting it to "customerA". Then you're clobbering the string variable with a new value -- one that's not valid since it's not a string (and compilation of that code would fail.) Pleas ALWAYS check that code compiles before posting it! Please read [How to ask](https://stackoverflow.com/help/how-to-ask), and how to post a [Minimal, Complete, Verifiable Example](https://stackoverflow.com/help/mcve). – Jeff Learman May 09 '18 at 17:33
  • I know, that this code is not working, but wanted to have a code 'description' of what want to do. – Chris May 11 '18 at 09:14
  • Read through the other comments and the answers of the existing question and will go with the Map solution. Thanks – Chris May 11 '18 at 09:15

2 Answers2

0

You can use map for this purpose. As i can understand, variable name depends on the value of the string but object type remains the same. You can do something like as follows :

String variableName = "name";
Map <String,Customer> customerMap = new HashMap <>();
customerMap.put (variableName,new Customer ());
Customer ref = customerMap.get (variableName);
ref.getName ();
yshdave
  • 1
  • 3
0

I would create a Factory Class for your customer objects. Then when you need an object of a certain type, you simply pass the String containing your desired class and the Factory returns the object.

public class CustomerFactory {

private CustomerFactory()
{
    //do nothing
}

public static Customer CreateCustomer(String customerType)
{
    if(customerType == null)
        return null;
    else if(customerType.equals("CustomerA"))
        return new CustomerA();
    else if(customerType.equals("CustomerB"))
        return new CustomerB();
    else
        return null;
}

}

This works as long as your Customer's all extend the parent Customer class. Then you could use the factory to give you back a class object of your choosing based on the String passed in like the following

CustomerA obj1 = CustomerFactory.CreateCustomer("CustomerA");
CustomerB obj2 = CustomerFactory.CreateCustomer("CustomerB");
RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26