Classes and Objects Fundamentals
An object created from a class is an instance of that class, and the variables it contains are called instance variables.
A Cookie Cutter Picture to Test
Object-oriented programming is built on two linked ideas: a class, which describes something, and an object, which is created from that class. A useful first picture is a cookie cutter. The cutter defines the shape, and each cookie pressed from it is an object. Before you read further, hold this picture in mind, because it is a helpful starting point and also a picture that breaks down in one important place.
Many beginners take the cookie cutter idea one step further than it should go. They assume every object is a full, independent copy of everything the class describes, including its behavior. Under that assumption, a class with an instance method that is used to create three objects would seem to leave three separate copies of that method's code sitting in memory, one inside each object. This article tests that assumption. The data part of the picture holds up well. The behavior part needs a correction, and the reason is precise.
Tracing the Data Inside Two Objects
When an object is created using a class, that object is called an instance of the class, and you can say that the object belongs to the class. The variables that the object contains are called instance variables. The word instance names the object, so an instance variable is simply a variable that lives inside one particular object.
The source illustrates this with a PlayerData class. When an object is created from PlayerData, that object is an instance of PlayerData, and the name and age variables are instance variables in that object. Trace what happens when two objects are created from the same class. The class is written once. Each object then has its own name and its own age.
Read the diagram as two separate containers. Object one holds the name Ana and the age 14. Object two holds the name Ben and the age 17. Both containers have a name slot and an age slot, because the class describes which variables an object will have. The values, however, belong to each individual object. That is why the two objects can hold different data at the same time.
What do you think happens?
Two objects are created from PlayerData. If you change the name stored in the first object, what happens to the name stored in the second object?
Reveal answer
The second object's name stays the same, because each object holds its own instance variables.
Instance variables are part of the object, not the class. The class describes which variables exist, but each object holds its own values for them, so the two objects can hold different data at the same time.
Why Instance Methods Belong to Objects
The same idea extends to behavior. The subroutines, which are the methods, that an object contains are called instance methods. The source states that instance methods are the non-static methods, and that they become part of objects created from the class. The source notes that its examples do not include methods, but that methods work similarly to variables. So the reasoning from the data section carries over, with one important qualification.
Because an instance method is logically part of the object, you can think of the object as carrying its behavior along with its data. The object contains both its instance variables and its instance methods in the logical sense. You are the one who calls a method on a particular object, and that object is the thing the method is attached to.
Read the concept map from top to bottom. The class creates objects, and each object is an instance of that class. The compiled code of the method is defined by the class, and the method is logically part of each object. The arrow from the object to the method is a statement about logical membership. It does not mean that each object stores its own physical copy of the compiled code for that method.
Static Versus Instance Methods
Not every method belongs to objects. The source contrasts two kinds. Static methods are part of the class. Non-static methods, which are the instance methods, become part of objects created from the class. The important fact is that static methods are associated with the class, not with objects. Keeping this contrast in mind prevents a common confusion about where a method lives.
The comparison makes the split visible. A static method is attached to the class itself, so you do not need an object to belong to it. An instance method is attached to the objects created from the class, so it is tied to an individual object. Both kinds of method are defined in the class, but they differ in which thing they belong to.
Common Mistakes About Where Things Belong
Common mistake Assuming each object stores its own copy of an instance method's compiled code
What it looks likeCreating three objects from a class with an instance method and expecting three separate copies of that method's code in memory.
Why it failsThe source states that it is not literally true that each object contains its own copy of the compiled code. The class defines the method once, and the method logically belongs to each object.
Do this insteadThink of the method as logically part of each object, while the compiled code is a single thing the class defines.
Common mistake Thinking all instance variables share one value across objects
What it looks likeAssuming that changing name in one PlayerData object changes name in every other PlayerData object.
Why it failsInstance variables are part of the object, not the class, so each object holds its own values.
Do this insteadTrack values per object. Two objects from the same class can hold different data at the same time.
Common mistake Treating a static method as if it belongs to each object
Why it failsThe source says static methods are part of the class and are associated with the class, not with objects.
Do this insteadAsk first whether the method belongs to the class or to objects created from it, then reason from that answer.
Practice: Sorting Members by Owner
Consider the PlayerData class from the source, which has instance variables name and age. Two objects, one named Ana aged 14 and one named Ben aged 17, are created from it. Then a static method and an instance method are also defined in PlayerData. For each of the following, decide whether it belongs to the class, to each object, or to both in the sense described by the source: the name variable in the Ana object, the age variable in the Ben object, the instance method, and the static method. Explain each answer in one sentence.
Hints
- Instance variables are part of the object, not the class.
- The source says instance methods become part of objects created from the class, but their compiled code is not physically copied into each object.
- Static methods are part of the class and are associated with the class, not with objects.
Summary of Ownership
- 1An object created from a class is an instance of that class, and the variables it contains are called instance variables.
- 2Instance variables are part of the object, not the class, so each object holds its own values for them.
- 3Instance methods are logically part of each object, but the object does not literally store its own copy of the compiled code.
- 4Static methods are part of the class and are associated with the class, not with objects.
At a glance
- An object created from a class is an instance of that class, and the variables it contains are called instance variables.
- Instance variables are part of the object, not the class, so two objects from the same class can hold different values at the same time.
- Instance methods logically become part of each object, but the compiled code is a single thing the class defines, not a copy stored in each object.
- Static methods are part of the class and are associated with the class, not with objects.