Increment variable names? [duplicate]

You can’t do this in Java and more importantly, you don’t want to do this as this isn’t how Java works. In fact variable names aren’t nearly as important as you think and hardly even exist in compiled code. What is much more important is that you are able to get a reference to your objects in as easy and reliable a way as possible. This can involve an array, an ArrayList (likely what you want here), a LinkedList, a Map such as a HashMap, a Set, and other types of collections.

For example:

List<Taco> tacoList = new ArrayList<Taco>();
for (int i = 0; i < MAX_TACOS; i++) {
   tacoList.add(new Taco(i));
}

Leave a Comment