# How to name objects with variables?

**URL:** https://discourse.processing.org/t/how-to-name-objects-with-variables/1027
**Category:** Coding Questions
**Created:** [June 17, 2018, 2:22pm UTC](https://discourse.processing.org/t/how-to-name-objects-with-variables/1027 "2018-06-17T14:22:51Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![RipplB](https://avatars.discourse-cdn.com/v4/letter/r/e68b1a/32.png) [@RipplB](https://discourse.processing.org/u/RipplB)
#### Post date: [June 17, 2018, 2:22pm UTC](https://discourse.processing.org/t/how-to-name-objects-with-variables/1027/1 "2018-06-17T14:22:51Z")

</div>

I have a class for a game, and i want to create more objects from that class. For that, i’d like to use a for loop. How to name the separate objects correctly? It is important, that later on i’d like to use their functions too.

---

<div class="post-metadata">

### Author: ![matthewjohnjamieson](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@matthewjohnjamieson](https://discourse.processing.org/u/matthewjohnjamieson)
#### Post date: [June 17, 2018, 3:16pm UTC](https://discourse.processing.org/t/how-to-name-objects-with-variables/1027/2 "2018-06-17T15:16:13Z")

</div>

Well, let’s say you want to make a Player class for a multiplayer game. The most naive way to instantiate them would be

```auto
Player p1 = new Player();
Player p2 = new Player();

```

and so on, which is tedious and what you’re trying to avoid. To put this in a loop, one way to do it is to use an array

```auto
Player players[];//declare the array
players = new Player[4];//create the array references (not the objects)

//loop initializes the objects in the array
for(int i = 0; i < players.length; i++ ){
  players[i] = new Player();
}

//objects can now be addressed individually by index...
players[0].move();
players[3].move();

```

If you aren’t sure how many objects you’ll need to store, look into the ArrayList data structure. If you want to address them by key-value pairs, look into hashmaps.
