Concepts / Understanding Objects, Classes, and Instances

Understanding Objects, Classes, and Instances

A class is a blueprint, or factory, used to construct objects; it describes what objects will contain rather than being the objects themselves

  • Programming

Two Houses From One Plan

You have already spent several chapters working with classes. A class can hold variables and methods, and you have written subroutines inside classes many times. Objects are built from the same ingredients, so it is natural to wonder how an object differs from the class it came from. Early in a course the difference can seem small. Once a program creates many objects from one class, and creates and discards them while it runs, the difference becomes important. This article traces that difference step by step, so you can reason about your code with confidence.

What do you think happens?

A class is defined in your program. Is the class itself one of the objects that the program creates while it runs?

  • Yes, the class is the first object the program creates

  • No, the class is a description, and the objects are separate things built from it

Reveal answer
Answer:

No, the class is a description, and the objects are separate things built from it.

The source describes a class as a blueprint or factory. A blueprint for a house does not contain a house. The class describes what objects of its kind will contain, and the objects are the separate things the description produces.

Why Objects Are Not Members of a Class

Most people first hear that objects belong to classes. They picture this relationship the same way they picture a member variable belonging to its class. That picture feels reasonable, but it hides the most useful fact about the relationship. A member variable is part of the class's own definition. An object is a separate thing that the class helps to produce.

The phrase objects belong to classes is a common shorthand, and it is worth being careful with it. The word belong suggests ownership of a part, as when a variable is a piece of its class. What actually happens is construction of a new item. The class is used to construct objects, and each object that results is its own item with its own values.

A class is a blueprint, or factory, used to construct objects. It describes what objects will contain rather than being the objects themselves. Think of a blueprint for a house. The blueprint does not contain a house, and it does not live in a neighborhood. It specifies what the house will have: its rooms, its doors, and where the walls go. Each house built from that blueprint has the same layout, yet each is a separate building that can be painted a different color or furnished differently. A class plays the same role for objects.

Tracing the Class and Its Objects

The clearest way to see the difference is to compare what exists in the program at a given moment. The class is one description. The objects are several separate items that exist only while the program is running. The diagram below places the single class description beside the objects it produces. Notice that there is one description on the left and many items on the right, and that the arrows show the description being used to build each object.

Class descriptionthe blueprintObject 1built from the classObject 2built from the classObject 3built from the classproducesproducesproduces
How is the single class description different from the separate objects that exist in memory during execution?

The source emphasizes that objects are created and destroyed as the program runs. The class, by contrast, is the description that stays in place. You can think of the class as a fixed plan that exists before the program does anything interesting, and the objects as temporary products that appear when the program asks for them and disappear when they are no longer needed. Because of this timing difference, one class can produce many objects over the lifetime of one program, and those objects do not all need to exist at the same moment.

How the Non-Static Parts Shape Each Object

The description inside a class is split into parts. The non-static parts are the ones that matter for building objects. They specify which variables and methods each object will contain. When the source says that the non-static portions of a class describe objects, it means that those portions set out the structure every object created from that class will have. Every object made from the class gets the same set of variables and methods, even though each object holds its own values for them.

Object 1its own variables andmethodsObject 2same variables and methods,different valuesObject 3same variables and methods,different values
What variables and methods does each object hold, and how do two objects from the same class differ?

The diagram shows three separate objects. Each one contains the same kinds of variables and methods, because the non-static parts of the class specify them. What differs is the values stored in each object's variables. The objects are separate items, so changing the values in one does not change the values in another.

The Factory Steps Behind Each New Object

The factory picture is useful because it turns the relationship into a sequence. Each time the program asks the class for a new object, the class uses its description to construct an item with the structure it specifies. The following diagram follows that sequence for two requests. Read it from top to bottom, since each message happens after the one above it.

Programrunning codeClassthe blueprintNew objectbuilt to the description1. request a new object2. construct from the description3. return the object4. request another object5. construct another with the same structure
What happens step by step each time the program requests a new object from the class?

Both requests use the same description, so both objects have the same structure. The class itself is not consumed by the request. It stays in place, ready to construct another object whenever the program asks. Later, when an object is no longer needed, it can be discarded, which is the other half of the lifecycle the source describes.

A Worked Example With Two Houses

Worked example

Building two objects from one class description

A class describes a house with a number of rooms and a method that reports its color. A program builds two houses from this class and gives them different colors. Explain what is shared and what is separate.

  1. 1
    Identify the class

    The class is the description. It names the variables and methods that every house will contain, such as a room count and a color.

  2. 2
    Identify the non-static parts

    The non-static parts are the room count and the color variable together with the method that reports the color. These parts specify what each house contains.

  3. 3
    Construct the first object

    The program asks the class for a house. The class builds an object with the rooms and methods it specifies. This object is the first house.

  4. 4
    Construct the second object

    The program asks the class again. The second house has the same structure, but its color variable can hold a different value than the first house.

  5. 5
    Compare the results

    Both houses share the layout the class describes. Each house is a separate item with its own values, so one house being painted differently does not change the other.

Result

One class produced two separate objects. The structure came from the class, and the values came from each object.

Illustrating the Idea in Code

The following example is an original illustration written for this article. It is not taken from the source. It uses Java-style syntax to show a class whose non-static variable is set separately for each object. The class is the description, and each call that builds a House produces a separate object.

Code walkthrough
class House {
    int rooms;
    String color;

    String describe() {
        return color + " house with " + rooms + " rooms";
    }
}

House first = new House();
first.rooms = 3;
first.color = "blue";

House second = new House();
second.rooms = 5;
second.color = "green";
  1. 1
    class House declares the description. It does not create any house by itself.
  2. 2
    The variables rooms and color are non-static parts. Every House object gets its own copy of them.
  3. 3
    The method describe is also a non-static part, so every House object can call it.
  4. 4
    new House() asks the class to construct an object. This happens while the program runs.
  5. 5
    first and second refer to two separate objects. Setting first.color does not change second.color.
  • The class House is the blueprint. It declares the non-static variables rooms and color and the method describe.
  • Each House object contains its own rooms and color variables.
  • The first object and the second object are separate items built from the same description.

Mistakes Beginners Make With Objects and Classes

  • Common mistake

    Treating an object as a part of its class, the same way a member variable is part of the class

    What it looks like

    Saying that an object belongs to its class and thinking the object is stored inside the class description

    Why it fails

    A member variable is part of the class's own definition. An object is a separate item that the class helps to produce.

    Do this instead

    Describe the relationship as construction: the class is used to construct objects, and each object is its own item.

  • Common mistake

    Believing that the class is the same thing as one object

    What it looks like

    Assuming that the blueprint for a house is itself a house

    Why it fails

    A blueprint for a house does not contain a house. The class describes what objects will contain and is not one of the objects it produces.

    Do this instead

    Keep the description and the products separate in your mind. One class can produce many objects.

  • Common mistake

    Assuming that objects last for the whole program

    What it looks like

    Expecting every object created from a class to exist at the same time for the entire run

    Why it fails

    The source emphasizes that objects are created and destroyed as the program runs, and they do not all need to exist at the same moment.

    Do this instead

    Track when each object is created and when it is no longer needed, rather than assuming they all persist.

  • Common mistake

    Expecting every object to share its values with the others

    What it looks like

    Changing the color of one house and expecting every house built from the class to change color

    Why it fails

    Each object is a separate item with its own values. The class sets the structure, not the values.

    Do this instead

    Remember that non-static variables give each object its own storage for its values.

Keeping Static and Non-Static Parts Apart

The source notes that early in a course you may have worked with an object only once, and it seemed to make little difference whether a subroutine was defined with the word static or without it. The distinction matters more once many objects are created. The non-static parts of a class are the ones that determine what each object contains. A member that belongs to the class as a whole is handled differently from a non-static part, so it is worth checking which kind of member you are writing before you rely on it inside many objects.

Practice: Counting the Objects

EASY

A class describes a bicycle with two wheels and a bell. A program creates three bicycle objects from this class, then discards one of them before creating a fourth. Work out how many bicycle descriptions exist, how many bicycle objects were created in total, and how many exist at the end of the program. Explain how you reached each answer using the idea that a class describes objects and objects are created as the program runs.

Hints
  • Count the class separately from the objects it produces.
  • Creation and discarding happen as the program runs, so count the events in order.
  • The class is a description that stays in place, and it is not discarded when an object is discarded.

Key Takeaways

  1. 1
    A class is a blueprint, or factory, used to construct objects. It describes what objects will contain rather than being the objects themselves.
  2. 2
    The non-static parts of a class specify the variables and methods that every object created from that class will contain.
  3. 3
    Objects are created and destroyed as the program runs, while the class remains the description that stays in place.
  4. 4
    One class can produce many objects with the same structure, and each object is a separate item with its own values.
  5. 5
    Objects are constructed from a class rather than being parts that belong to it in the way a member variable does.

At a glance

  • A class is a blueprint or factory: it describes what objects will contain and is not itself one of the objects it produces.
  • The non-static parts of a class specify the variables and methods that each object created from it will contain.
  • Objects are created and destroyed while the program runs, and the class remains in place to construct more objects.
  • One class can produce many separate objects with the same structure, each holding its own values.