How to define a java object name with a variable?

Your choice of words shows that you don’t fully understand the way variables and objects work, and you need to fix that to get anywhere with Java.

If I write:

Item myItem = new Item();

I create a new object, and I define a variable which points to that object.

The object does not have a name (it has an ID, which is a number assigned at runtime; most programmers can ignore it).

The variable has a name, myItem. The variable points to the object. Later on, it might point to a different object. The variable might fall out of scope and cease to exist, while the object continues to exist.

You might say, “that’s not important. If I say the ‘name of the object’, I mean ‘the name of the variable pointing to the object’.”

But it is important, because lots of objects will be pointed to by more than one variable:

Item myItem = new Item();
Item yourItem = myItem;
// both variables point to the same object. Neither variable is "more" the
// owner of the object than the other.

… and lots of objects won’t be pointed to directly by a variable.

myList.append(new Item()); 
// you can't get at this new object using a direct variable.

In Java (and most mainstream languages), you can’t create variable names at runtime. The only variable names that exist, are ones that are literally in the source code.

So there’s nothing that works like this:

int number = 1;
Cell cell$number = new Cell(); // can't be done!
Cell currentCell = cell1; 

Nor can you access variables by name, based on runtime data. So there’s nothing that works like this:

Cell cell1 = ...;
int number = 1;
Cell currentCell = cell$number; // can't be done!

This is a good thing because it lets the compiler validate certain things about your code.

In some languages you could possibly achieve something like this using eval() — but you should run a mile from it!

This is a hint that if you’re writing code like:

 Cell cell0 = new Cell(...);
 Cell cell1 = new Cell(...);
 ...
 Cell cell99 = new Cell(...);

… then you’re doing something wrong. It may work, but it scales badly. There is no way to put this into a loop. (Don’t worry – most of us hit this issue when we began programming).

If, instead you’d used an array:

 Cell[] cells = new Cell[];
 cells[0] = new Cell(...);
 cells[1] = new Cell(...);
 ...
 cells[99] = new Cell(...);

Then you could use a loop instead:

 for(int i = 0; i<100; i++) {
     cells[i] = new Cell(...);
 } 

An array is the simplest of “collection” objects. There are also Sets, Lists, Maps, and a host of more advanced collections, to suit most needs.

You seem to want to store and access objects using strings as a key. The way to do this is using a Map.

Map<String,Cell> myMap = new TreeMap<String,Cell>();

myMap.put("AA", new Cell(...));
myMap.put("AB", new Cell(...));

...

Cell currentCell = myMap.get("AA");

Map is an interface. TreeMap is just one class that provides an implementation of the Map interface. Read up on the various implementations of Map provided by the standard JRE.

The key doesn’t have to be a String. Anything that implements equals() can be used as a key.

Using this:

for(char c="A"; c <= 'L'; c++) {
   for(char d = 'A'; d<= 'L'; d++) {
       myMap.put("" + c + d, new Cell(...));
   }
}

However, since you’ve said you want a grid, it’s probably better to work with a 2D array, and translate the numeric coordinates to and from a lettered grid-reference whenever you need to. Google for “2D array Java”, and you’ll find plenty of examples.

Cell[][] cells = new Cell[12][12];
for(int x=0; x<12; x++) {
   for(int y=0; y<12; y++) {
       Cell cell = new Cell();
       cell.setName( "" + ('A' + x) + ('A' + y)); // perhaps
       cells[x][y] = cell;
   }
}

Beyond the standard JRE, there are plenty of other implementations of Map. For example, Spring provides DefaultRedisMap, in which the objects are stored in a Redis database.

More generally – you have asked a very basic question here. You should read the chapter on the Collections API in any decent Java book.

Leave a Comment