Objects and Their Classes
Classes have a dual role: they can contain static members and describe objects.
Why One Word Means Two Things
A common first impression is that a class has only one job. Some learners think a class is simply a place to keep static variables and subroutines. Others think a class is only a description used to build objects. Java does not force a choice between these. In Java, a class can serve both purposes, and this dual role is useful. It can also be confusing, because the single word class refers both to a container and to a description of object structure.
This article follows one question through the rest of the lesson: when you see the word class in Java code, which of its two jobs is being used? To answer that, we will trace what exists when a class is written, what exists when objects are created from it, and what each object gets to keep for itself.
Tracing Two Objects From One Class
Before reading any definitions, start with a concrete case. Suppose a class called Student describes a name and a score, and a subroutine called report that prints them. Two Student objects are created. Each one has a name and a score of its own. Both objects can call report. Ask yourself: what do the two objects share, and what is different between them?
What do you think happens?
If two Student objects are created from the same class, and one is given the name Ana while the other is given the name Ben, will both objects show the same name?
Reveal answer
No, each object keeps its own name value
The class defines that each object has a name variable, but each object can hold its own value for that variable. Sharing a class does not mean sharing values.
Writing the Class That Describes Objects
The class is the description. It states which variables each object will contain and which subroutines each object can use. The following class is an original example written for this article. It describes a student with a name and a score, and a subroutine that reports both values.
- String name and int score are variables the class defines for each object
- void
report()is a subroutine the class defines, available to each object
Notice what this class does not do. It does not create any student. It only states the structure that a student object will have. The words name, score, and report belong to the description. Values such as Ana or 90 only appear when an object is actually created and given its own values.
The Dual Role of a Class
The first job of a Java class is to act as a container for static members. Static variables and static subroutines belong to the class itself. They can be used without creating any object at all. The second job is to describe objects. The class states what variables and subroutines each object created from it will contain. The same class can do both jobs at once: it can hold static members and also serve as the blueprint for objects.
Using a Class Name as a Type
A class name can also be used as a type. When a variable is declared with a class name as its type, that variable is meant to hold objects of that class. In the Student example, a declaration such as Student a means that a is intended to hold a Student object. The class name is doing the job of a type, in the same way that int tells Java a variable holds integers.
The diagram shows the idea without committing to any detail the source does not give. Each variable is declared with the type Student, and each one is associated with a separate object. The variable is not the object; it is the name by which the program refers to an object of that class.
Shared Structure and Individual Values
Here is the Student class in use. Two objects are created, each given its own values, and each asked to report. The code is an original example written for this article.
Ana scored 90
Ben scored 75- Both objects were created from the same class, so both have a name and a score
- Each object holds its own name and score values
- Both objects use the same report subroutine defined in the class
The three shared items come from the class description, so every Student object has them. The values in the other two columns are the individual part. Changing a.score to 95 would change object a only. Object b would still hold 75. The structure is shared; the values are not.
Mistakes Beginners Make With Classes and Objects
Common mistake Thinking a class and an object are the same thing
What it looks likeWriting class Student and then expecting Student to already have a name value
Why it failsThe class is the description. It states that each object will have a name, but no particular name exists until an object is created and given one.
Do this insteadCreate an object from the class before assigning or reading its values.
Common mistake Assuming all objects of a class share their variable values
What it looks likeBelieving that setting a.score to 90 also changes b.score
Why it failsObjects of the same class share the structure defined by the class, but each object can hold its own variable values.
Do this insteadTreat the class as shared structure and each object's variable values as individual data.
Common mistake Reading every member of a class as belonging to each object
What it looks likeAssuming a static variable is copied into every object
Why it failsStatic variables and static subroutines belong to the class itself, not to each object the class describes.
Do this insteadCheck whether a member is static before deciding whether it belongs to each object.
Uniform Handling Through Shared Subroutines
The source uses the PrintStream example to show how different objects can share a common subroutine such as println. The key idea is that a subroutine name can be a common interface. When objects share a common characteristic, such as having a println subroutine, code can call that subroutine in the same way regardless of which particular object it is working with.
This is what makes uniform use possible. A program does not need a separate calling pattern for each object that offers the same kind of subroutine. It calls the common subroutine, and each object carries out that operation according to its own structure and values. The class description is what guarantees that the shared subroutine is there in the first place.
Practice: Reading a Class Description
Consider a class Book with a title variable, a pages variable, and a subroutine called describe. Create two Book objects with different titles and page counts. Then answer: which items are shared structure, and which are individual values? Finally, decide whether a static variable named libraryCount, if added to Book, would belong to each Book object or to the class itself.
Hints
- Shared structure comes from the class description, so title, pages, and describe are shared as structure
- Each object's title and page count are its individual values
- Static members belong to the class itself rather than to each object
Summary of the Class and Object Relationship
A Java class has a dual role. It can contain static variables and subroutines that belong to the class itself, and it can describe the objects created from it. The class name can also be used as a type for variables that hold objects of that class. The class defines the variables and subroutines each object contains, while each object can hold its own variable values. Objects of the same class share that structure, which is why common subroutines such as println can be used across different objects in a uniform way.
At a glance
- A Java class can act as a container for static variables and subroutines and as a description for objects.
- A class name can be used as a type for variables that hold objects of that class.
- The class defines which variables and subroutines each object contains.
- Objects of the same class share structure, but each object can hold its own variable values.
- Common subroutines such as println let different objects be used in a uniform way.