Object Life Cycle

This post will discuss object life cycle.

Know that the word “construct” always refers to object life cycle.

object lifecycle

The primary purpose of the constructor is to set up some instance variables to have the proper initial values when the object is created.

The constructor and destructor are optional. The constructor is typically used to set up variables. The destructor is seldom used.

Consider the following class from the last post, but with two new methods.

object example

Most of the methods with double underscores are for code that gets run when certain occurrences happen in the language.

The only time the thing gets destructed is at the end of the program.

The execution of this program is quite simple.

program execution

The constructor is a special block of statements when the object is created.

Now, consider having more than one instance.

many instances

In the following code, you have two instance variables, a constructor, but no destructor.

two instances

The above program will create two independent instances.

Leave a Reply

Your email address will not be published. Required fields are marked *