Object Life Cycle

One way of thinking of objects is that they have a life cycle. All of these things happen in a file outside the object’s class. The class is merely code that the object will use.

Event

Description

Example

Declaration

Planning which type of object you will have.

Button exit;

Construction

Getting the instance variables and memory ready to use.

Exit = new Button (“Hi”);

Accessors/ Inspectors

Return the values of the instance variables.

Generally named get---

A = exit.getLabel();

Mutators

Changes or mutates the values of the instance variables.

Generally named set---

Exit.setLabel(“Yippee”);

Facilitators

Does something useful. These are generally the most difficult methods to code.

Button doesn’t really have any. CompareTo is a pretty good example of one. If (exit.getLabel().compareTo (“Turkey”)==0) …

Destructors

Clean up memory when the object is not longer used.

You don’t need to code this in Java, the compiler does it for you!!

Here is an analogy to help you remember the object life cycle. The first one is building a house. The declaration occurs when the builders make the blueprints. The constructor occurs when the house is being built. A mutator is sort of like a family moving into a house. The family living in the house can change. A facilitator is things that the house can do: have its grass mowed, get new paint, have mailed delivered, build a garage. A accessor is information about the house which is made available – say the address painted by the front door.  The house is destroyed when the bulldozers come in to make way for a new subdivision.

Questions

  1. Look at the Robot Object. Find as many stages of the Object Life Cycle in it as you can. Record a method name for each stage of the Object Life Cycle.
  2. Look at the Dice Object. Find as many stages of the Object Life Cycle in it as you can. Record a method name for each stage of the Object Life Cycle.
  3. Write another analogy for the Object Life Cycle. Try something living, like a caterpillar.