Sub topic 1
Classes as definitions, objects as instances
A Java program often needs to work with things such as books, bank accounts, or game characters. A class describes a kind of thing the program can work with. An object is one thing created from a class.
Think of a class as a plan for a mug. The plan describes a kind of mug. An actual mug made from the plan is one object. Two mugs made from the same plan are two objects, even though both follow the same plan.
In Java, an object created from a class is also called an instance of that class. If a class is named Mug, then a Mug object is an instance of Mug.
Why use classes and objects?
A program may need to handle many things of the same kind. A library program, for example, may track thousands of books. Instead of writing a separate description for every book, the programmer can define a Book class and use it to make book objects.
The class gives the program one way to describe books. Each object can then stand for a particular book.
A class can describe both:
- Data: information the program uses, such as a book’s title.
- Behavior: things an object can do in the program, such as report that it is on loan.
The class describes what its objects can have and do. The objects are the individual things the program works with. For example, one book object can represent a copy of a novel, while another represents a copy of a cookbook.
You do not need to know how to add data and behavior to a class yet. For now, focus on the difference between the description and the things described by it.
One class, many objects
A single class can have many instances. Each instance is a separate object.
The two objects are both instances of Mug. That does not make them the same object. Just as two mugs can come from the same plan, two objects can come from the same class.
A class can also exist without the program creating any objects from it. Defining the Mug class does not, by itself, give the program a mug object to use.
A simple class definition
Here is a class with nothing written inside its body:
class Mug {
}The word class tells Java that this is a class definition. Mug is the class’s name. The braces, { and }, mark the start and end of the class body. The class body is where the description of the class goes.
This definition gives Java a class named Mug, but it does not create a Mug object. It is like drawing up a plan without making a mug from it.
Java class names often start with a capital letter. This makes names such as Mug easier to spot in a program.
Worked example: Two instances of one class
The following complete program defines Mug and creates two Mug objects. It then checks whether each object is an instance of Mug.
To run it, you need a Java Development Kit, or JDK. A JDK is a set of tools for writing and running Java programs.
Save this code in a file named Main.java:
class Mug {
}
public class Main {
public static void main(String[] args) {
Mug firstMug = new Mug();
Mug secondMug = new Mug();
System.out.println("firstMug is a Mug: " + (firstMug instanceof Mug));
System.out.println("secondMug is a Mug: " + (secondMug instanceof Mug));
}
}Open a terminal in the folder containing Main.java. A terminal is a place where you type commands for your computer to run. Enter:
javac Main.java
java MainThe first command uses the Java compiler, a tool that checks and prepares Java code to run. The second command runs the program. You should see:
firstMug is a Mug: true
secondMug is a Mug: trueRead the example
The file contains two class definitions:
Mugdescribes the kind of object we want to create.Mainholds the instructions that start this program.
The line beginning with public static void main is the program’s starting point. A method is a named set of instructions that a program can run. For this example, the important part is what happens inside the main method.
Look at the first line inside it:
Mug firstMug = new Mug();new Mug() creates a Mug object. The name firstMug is a variable: a named place the program uses to refer to a value. Here, it lets the program refer to the new object.
The next line creates another Mug object and lets the program refer to it as secondMug. These are two separate objects. Both are instances of the same Mug class.
The example also uses instanceof. This word asks whether an object is an instance of a given class. The answer is true or false. Both checks print true because both objects were created as Mug objects.
The checks tell us that the objects belong to the class. They do not, by themselves, count the objects. We know there are two separate objects because the program creates one with each of the two new Mug() expressions.
Classes are not limited to physical things
The mug plan is a useful way to picture a class, but Java objects do not have to represent physical items. A class can describe something less tangible, such as:
- A
Reservationfor a seat. - A
Scorein a game. - A
Messagesent between people. - A
Timerin an app.
For example, a program that handles reservations might have a Reservation class. One Reservation object could represent a seat booked for Monday, and another could represent a seat booked for Tuesday.
In each case, the class is the general definition. An instance is one object the program uses under that definition.
Choose what an instance represents
When designing a class, it helps to ask: “What will one object stand for?”
Imagine a library program. The answer for a Book object depends on what the program needs to track:
- If it tracks titles in a catalog, one
Bookobject might represent a title. - If it tracks items people borrow, one
Bookobject might represent a particular physical copy.
Those choices are different. A library can own several copies of the same title. If the program needs to track each copy on loan, treating every copy as a separate object may be useful.
There is no need to design the whole library program now. The point is that a class gives objects a shared description, while the programmer decides what each object represents.
Keep the distinction clear
| Term | Meaning | Example |
|---|---|---|
| Class | A definition of a kind of object | The `Mug` class |
| Object | An individual thing used by the program | The first mug created in the example |
| Instance | An object described in relation to its class | The first mug is an instance of `Mug` |
“Object” and “instance” often refer to the same thing. Use instance when you want to say which class an object belongs to.
The distinction matters when reading code. Seeing a class definition tells you what kind of object the program can use. Seeing an object being created tells you that the program now has an individual instance to work with.
Exercises
Exercise
A program defines a class named Ticket but creates no Ticket objects. How many Ticket instances has it created?
Show answer
Zero. Defining a class does not by itself create an object.
Exercise
A program creates three objects from the Ticket class. Are they one object or three? What class is each an instance of?
Show answer
They are three separate objects. Each is an instance of Ticket.
Exercise
A library owns two copies of the same novel and needs to track who borrows each copy. What could one book object represent?
Show answer
One object could represent one physical copy. Then the program could track the two copies separately.