A poet is a professional maker of verbal objects. [W.H. Auden]
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 for that object. Why is it important that we encapsulate information inside an object and work with instances of objects 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 PinkElephantmyPet = new PinkElephant();
create a new instance of PinkElephant,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();
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.
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).
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.