Object-Oriented JavaCore· 35 min read

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.

ClassObject
A blueprint or templateA real thing built from the blueprint
Written onceCreated many times with new
Dog (the idea of a dog)rex, bella (actual dogs)
Describes fields & methodsHolds its own field values
A class with two fields and one method
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.

Building two Dog objects from one class
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:

this points to the object the method belongs to
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?

Answer: A class describes a kind of thing once. You create many objects from it using new, and each object has its own field values.

✍️ Practice

  1. Write a Book class with fields title and pages and a method describe() that prints them.
  2. In main, create two Book objects and call describe() on each.

🏠 Homework

  1. Design a Car class with fields (brand, speed) and a method accelerate() that increases speed and prints it. Make two cars and drive them.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →