Goals

  • Using Objects
    • Declaring / Initializing Objects
    • Array of Objects
    • ArrayList of Objects

Resources

http://processing.org/learning/topics/arraylistclass.html

"A poet is a professional maker of verbal objects." ~ W.H. Auden

Declaring an Object

Once an object is defined by a class definition, it can be instantiated, bringing an instance of that object into your program, allowing you to change fields and call methods. Why is it important that we encapsulate information inside an object and work with instances of the object rather than working with the data directly? We do this to make our code both formally and conceptually cleaner. Remember working with strings? We never had to know the details of the methods of the string class, or how they worked, we leveraged code that was already written. This is the power of object oriented programming.

PinkElephant myPet; declare a reference variable of type PinkElephant
myPet = new PinkElephant();

  • Creates a new instance of the PinkElephant class by calling the constructor in the PinkElephant class.
  • A reference to the newly created object is returned to where the constructor was called using the new keyword.
  • The reference of the newly created object is assigned to the myPet reference variable.

Now that we have a reference variable of the type PinkElephant, we can go ahead and change some of this objects fields or call some of it's methods.

myPet.size = 50; set the size of myPet (a PinkElephant) to 50
myPet.eatPeanuts(); tell the myPet object to eatPeanuts();

When you create an instance of a class (an object) using the new keyword, you are actually calling the constructor method of that class, which will return a reference to the newly created object.
Make sure you declare a reference variable of the right type (class name) before you create a new instance of that object.

Array of Objects

We've seen before that creating multiple elements of the same type can be tedious and time consuming in code. It's much better to store similar primatives or complex (object) types in an array that can be controlled by a loop. Here's how to declare an array of objects:

PinkElephant[] myPets;
Declare an array of PinkElephant objects.
myPets = new PinkElephant[5];
Initialize the array of PinkElephant objects.
NOTE: No instances of PinkElephant have been created yet...
Only the array has been initialized, however each position is still empty (NO PINK ELEPHANTS)

Now let's initialize each position in our array with a new instance of the PinkElephant class:

Now what if we wanted to draw each of our "pets" of the type PinkElephant. Well first we would want to make sure the PinkElephant class has a draw method. For the purposes of this demonstration let's assume it does.

So we've successfully called the draw method of each of our pets.

When we declare and initialize an Array of objects, the Array is empty. it has a size, but doesn't contain any objects.
Not until we've initialize each position in the array can we say that our array contains objects.
Once each position in the array has be initialized with a new instance of the object class, we can begin using those newly created objects in other portions of our sketch.

ArrayList of Objects

Now we will go into the details of creating an ArrayList of objects. For some purposes an ArrayList is better than an Array. Mainly if the size of your list is going to expand and contract, an Array won't be suited for this. ArrayLists have the added benefit of not having a fixed size, which allows you to add and remove items when you wish.

ArrayList myPets;
Declare an ArrayList.
myPets = new ArrayList();
Initialize the ArrayList.
NOTE: ArrayLists are type agnostic by default (they do not know what kind of objects they contain)

Now let's add some new instances of the PinkElephant class to our ArrayList:

Notice the use of the add( *object instance* ) method of the ArrayList. The ArrayList itself is an object, and add(...) is merely a method of the ArrayList class.
Also note that when creating the ArrayList we never specified a size, so in our first for loop, we cannot use the ArrayList method size() yet, since the ArrayList myPets contains no objects before the loop.

Let's draw our PinkElephants inside the ArrayList myPets.

Notice how we declared a new reference variable to store each reference we took out of our ArrayList using the get(index) method? This is merely a formality, and helps for calling other methods of each object in the ArrayList.

Since ArrayLists are type agnostic, the ArrayList does not remember that we added PinkElephant objects into it. So when we use the get(index) method of the ArrayList it returns a reference to an object of the generic type Object, not PinkElephant.

In order to use the reference variable returned by the get(index) method as a PinkElephant, we must first cast it as such using the cast (PinkElephant).

Typed ArrayList

We can declare a typed ArrayList which will behave a lot more like an Array since the ArrayList will return references of the type we specify. Here is an example:

This last line where we get and use the object instance directly as if it was an object of PinkElephant only works because our ArrayList is typed. This is an extremely useful feature of ArrayLists.

A newly created ArrayList has a size of 0.
ArrayLists do not remember the type of objects they contain, so each object reference obtained using the .get(index) method must be cast before using.
A typed ArrayList will always return a reference of the type specified when the ArrayList was created, making life much more simple.