19

This is a simple question (I think)

Lets say I have this code (Assuming I have a dog class)

String name = "dog";
dog name = new dog();

How can I get java to recognize name as a String and name the object dog?

EboMike
  • 76,846
  • 14
  • 164
  • 167
charles horvath
  • 315
  • 1
  • 2
  • 9
  • 3
    Why do you want to do this? Surely there's a better way to achieve what you're trying to do. – Telmo Marques Dec 25 '11 at 22:43
  • 1
    You probably should learn about Java's reflection API if that is what you want. – fge Dec 25 '11 at 22:53
  • I think the title and the content of this question are inconsistent. It seems like you are *not* trying to create a variable whose name is based on the value of a string. Rather it seems like you want to set the class of a variable based on the string value. If the latter is the case, the `Factory` pattern might help. – Kavka Dec 25 '11 at 22:53
  • @fge: I think we need to know more about why he wants to do this. My experience here is that > 90% of the time reflection is not the solution to the over-all problem that the poster is trying to solve. – Hovercraft Full Of Eels Dec 25 '11 at 23:01

2 Answers2

48

While you can do what you're trying in some scripting languages such as PHP (and this question is often asked by many PHP programmers who start Java), this is not how Java works, and in fact variable names are a much less important than you may realize and hardly even exist after code is compiled. What is much more important and what is key are variable references -- the ability to gain access to a particular object at a particular point in your program, and you can have Strings refer to objects easily by using a Map as one way.

For example

Map<String, Dog> dogMap = new HashMap<String, Dog>();
dogMap.put("Fido", new Dog("Fido"));

Dog myPet = dogMap.get("Fido");

Or you can gain references to objects in many other ways such as via arrays, ArrayLists, LinkedLists, or several other collectinos.

Edit
You state:

The thing is that in my code I am going to be using one method to create objects, the name of the object is arbitrary but I need it to be dynamic because it wont be temporary, so the actually name of the object has to change or I will be writing over the previously declared object.

This is exactly what I meant when I said that the name of the variable is not as important as you think it is. The variable name is not the "object name" (this really doesn't exist in fact).

For example if you create a dog in a variable named Fido, and then assign it to a new variable named spot, both variables, despite having different names will refer to the very same object:

Dog fido = new Dog;
Dog spot = fido; // now fido and spot refer to the same object

If you want to give a variable a "name" consider giving the class a name property:

class Dog {
   private String name;

   public Dog(String name) {
      this.name = name;
   }

   public String getName() {
      return name;
   }
}

Now you can give each Dog object its own (semi) unique name if you wish.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • The thing is that in my code I am going to be using one method to create objects, the name of the object is arbitrary but I need it to be dynamic because it wont be temporary, so the actually name of the object has to change or I will be writing over the previously declared object. – charles horvath Dec 25 '11 at 22:52
  • Well, the described method in this answer allows you to do just that! – Zéychin Dec 25 '11 at 23:00
  • I am trying to get this to work, I will post back in about thirty or so to tell you how it goes. – charles horvath Dec 25 '11 at 23:19
  • If I can't get a hash map to work for me I will post the full info of what I am trying to do (It is kinda long) – charles horvath Dec 25 '11 at 23:19
  • @charleshorvath: please let us know how it goes -- and good luck!! – Hovercraft Full Of Eels Dec 25 '11 at 23:19
  • It all worked out good! I figured out how to use the HashMap to map references of the object. Thanks a lot – charles horvath Dec 27 '11 at 08:47
  • I know this is really old @HovercraftFullOfEels, but I hope you see this. What if there are 2 different key-value in the HashMap with 2 different instances? Say `map.put("Cat", new Cat())`, how do you declare the HashMap? `HashMap`? How do you instantiate the class? – bretonics Sep 20 '18 at 02:27
6

I don't suppose you are thinking of Enums?

private static void test () {
  Animal animal = Animal.valueOf("Dog");
}

enum Animal {
  Dog,
  Cat,
  Cow,
  Pig,
  Rat,
  Ant,
  Gnu;
}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213