← All lessons
Intermediate Java · Lesson 1 of 9

Classes and objects

In Java basics, a student's name and score lived in two separate arrays that had to stay lined up. Object-oriented programming fixes that by letting you make your own types. A class is a blueprint that says what data a thing has and what it can do; an object is one thing made from it. String is a class, and every piece of text is an object of it.

Here is a class for a student. Its fields are the data each student carries. Its constructor (the part with the class's own name and no return type) runs when an object is made and fills the fields in. Its methods are what a student can do.

Java
public class Main {
    static class Student {
        String name;
        int score;

        Student(String name, int score) {
            this.name = name;
            this.score = score;
        }

        boolean passed() {
            return score >= 50;
        }
    }

    public static void main(String[] args) {
        Student ana = new Student("Ana", 91);
        Student ben = new Student("Ben", 42);
        System.out.println(ana.name + " " + ana.passed());
        System.out.println(ben.name + " " + ben.passed());
        ben.score = 55;
        System.out.println(ben.passed());
    }
}
Prints
Ana true
Ben false
true

new Student("Ana", 91) makes a new object and runs the constructor with those values. Each object has its own copy of the fields: changing Ben's score doesn't touch Ana's. Inside the class, this means "the object this method is running on", so this.name = name copies the parameter name into this object's field name. (In these examples the class sits inside Main, marked static, so the whole program fits in one file. In a real project each class usually gets its own file, like Student.java.)

Methods like passed() use the object's own fields directly. You call them with a dot on an object: ana.passed() works out Ana's answer, ben.passed() Ben's.

toString

Printing an object shows something like Main$Student@5ca881b5: its class and a code number. Give the class a method called toString and Java uses it whenever the object is turned into text, in println and in +.

Java
public class Main {
    static class Point {
        int x, y;

        Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        void move(int dx, int dy) {
            x += dx;
            y += dy;
        }

        @Override
        public String toString() {
            return "(" + x + ", " + y + ")";
        }
    }

    public static void main(String[] args) {
        Point p = new Point(2, 3);
        p.move(1, -1);
        System.out.println(p);
        System.out.println("At " + p);
    }
}
Prints
(3, 2)
At (3, 2)

@Override tells the compiler you mean to replace a method every object already has (more on that in the inheritance lesson). If you misspell toString, the compiler then complains instead of silently ignoring it.

Variables hold references

A variable of a class type doesn't hold the object itself, it holds a reference to it, like an address. Copying the variable copies the address, so both names point at the same object:

Java
public class Main {
    static class Box {
        int value;
        Box(int value) { this.value = value; }
    }

    public static void main(String[] args) {
        Box a = new Box(1);
        Box b = a;
        b.value = 99;
        System.out.println(a.value);
        Box c = new Box(99);
        System.out.println(a == b);
        System.out.println(a == c);
    }
}
Prints
99
true
false

That's why == on objects asks "same object?" rather than "same contents?". A reference can also be null, meaning it points at nothing; calling a method on it throws a NullPointerException.

A class name starts with a capital (Student), fields and methods with a small letter (score, passed). Name a class for the one thing it represents.

Practice

What's the difference between a class and an object?

More info

Quick reference

class Name { fields; constructor; methods }A blueprint for objects.
new Name(...)Make an object and run its constructor.
fieldA variable each object has its own copy of.
constructorHas the class's name, no return type; fills in the fields.
thisThe object the method is running on.
toString()How the object prints; mark it @Override.
referenceWhat a variable of a class type holds; copying it shares the object.
nullA reference to nothing.

Related courses