Class

A class is the basic unit of code organisation in Java. It is a blueprint that declares the fields (data) and methods (behaviour) of the objects it describes. You create objects (instances) from a class using the new keyword.

Minimal class

public class Car {
    private String brand;
    private int year;

    public Car(String brand, int year) {
        this.brand = brand;
        this.year  = year;
    }

    public String getBrand() { return brand; }
}

Creating instances

Car myCar = new Car("Toyota", 2022);
System.out.println(myCar.getBrand()); // Toyota

Class vs object vs instance

Class = the definition. Object / instance = a concrete realisation of the class in memory. "Instance" and "object" are synonyms; you create instances of a class. One class typically has many instances.

Modern alternatives

For simple data carriers, prefer records (Java 16+). For fixed sets of named constants, use enums. For classes you want restricted to a known set of subtypes, use sealed classes (Java 17+).

See the full guide to classes for access modifiers, inheritance and constructors.