You already know that you can store data in an array. Storing objects is very similar. Here are the steps.

Create an array of objects

To store objects in an array give the name of the class as the data type. The code below will store ten Pet objects into an array called myPets.

Pet[] myPets = new Pet[10];

Add an object to the array.

To add data you now need to carry out two steps:

  1. Create an object:
Petp = new Pet();
  1. Store the object. The code below will store the new Pet object p into the first position in the array.
petNames[0]= p;

Retrieve the object from the array.

You could display each name like this:

Pet p = petNames[0];