Classes & Objects
A class is a blueprint; an object is a real thing built from it. This is the heart of Java.
What you will learn
- Define a class with fields and a method
- Create objects with new
- Use the keyword this
Blueprints and things built from them
Java is an object-oriented language. The big idea: describe a kind of thing once as a class (a blueprint), then make as many real objects from it as you need.
Think of a class Dog as the blueprint. Each actual dog — Rex, Bella — is an object built from that blueprint, with its own values.
A nice everyday picture: a class is like a cookie cutter, and objects are the cookies you stamp out with it. One cutter (the class) can make hundreds of cookies (objects). Each cookie is real and separate, but they all share the same shape defined by the cutter.
| Class | Object |
|---|---|
| A blueprint or template | A real thing built from the blueprint |
| Written once | Created many times with new |
Dog (the idea of a dog) | rex, bella (actual dogs) |
| Describes fields & methods | Holds its own field values |
public class Dog {
// fields: the data each dog has
String name;
int age;
// a method: something a dog can do
void bark() {
System.out.println(name + " says woof!");
}
}Note: Output: (No output yet — this is only the blueprint. Nothing runs until we build an object from it and call its method, which we do next.)
Creating objects with new
Use the keyword new to build an object from the class. Each object keeps its own field values.
public class Main {
public static void main(String[] args) {
Dog rex = new Dog();
rex.name = "Rex";
rex.age = 4;
Dog bella = new Dog();
bella.name = "Bella";
bella.age = 2;
rex.bark();
bella.bark();
System.out.println(bella.name + " is " + bella.age + " years old.");
}
}Note: Output:
Rex says woof!
Bella says woof!
Bella is 2 years old.
We used new Dog() twice to make two separate dogs. Each has its own name and age. The dot . reaches inside an object to its fields and methods.
The keyword this
Inside a method, this means the current object. It is useful when a parameter has the same name as a field:
public class Dog {
String name;
void setName(String name) {
this.name = name; // this.name = the field; name = the parameter
}
}Note: Output:
(No output — this snippet shows the pattern. this.name is the object field, plain name is the parameter. Without this, Java could not tell them apart.)
Tip: Vocabulary: fields are the data an object holds, methods are what it can do, and an object is one real example built with new. A class bundles related data and behaviour together.
Q. What is the difference between a class and an object?
new, and each object has its own field values.✍️ Practice
- Write a
Bookclass with fieldstitleandpagesand a methoddescribe()that prints them. - In main, create two Book objects and call
describe()on each.
🏠 Homework
- Design a
Carclass with fields (brand, speed) and a methodaccelerate()that increases speed and prints it. Make two cars and drive them.