EMZETT.
Login

Classes

In short: The blueprint for objects — defines which fields (state) and methods (behaviour) instances of this class have.

In more detail: A class itself is not yet a concrete object, but the template from which any number of objects can be created via new, each carrying its own values for the fields. In Java, exactly one public class usually lives per file, named the same as the file itself.

In Depth

public class Account {
    // fields = state per object
    private String owner;
    private double balance;
 
    // constructor - runs on every "new Account(...)"
    public Account(String owner, double startingBalance) {
        this.owner = owner;
        this.balance = startingBalance;
    }
 
    // methods = behaviour
    public void deposit(double amount) {
        this.balance += amount;
    }
 
    public double getBalance() {
        return balance;
    }
}
 
Account mine = new Account("Anna", 100.0);
Account yours = new Account("Ben", 50.0);
mine.deposit(20.0); // only affects "mine", "yours" stays untouched

Every instance (every object created via new) gets its own, independent set of instance fields — mine and yours have separate balance values, even though both come from the same Account class. Fields or methods marked with static, by contrast, belong to the class itself, not to an individual instance, and are shared by all objects together (e.g. a counter for how many Account objects have been created in total). The convention “one public class per file, filename = class name” is not a style tip in Java, but a rule enforced by the compiler.

static: shared state at the class level

class Account {
    private static int accountCount = 0; // belongs to the class, not an instance
    private String owner;
 
    Account(String owner) {
        this.owner = owner;
        accountCount++; // every new account increments the shared counter
    }
 
    static int getAccountCount() {
        return accountCount; // called via Account.getAccountCount(), not via an instance
    }
}

static fields exist exactly once per class, regardless of how many objects are created — a typical use case is counters, configuration values, or utility methods that don’t need their own object state (e.g. Math.max()).

Access modifiers as an encapsulation tool

In well-structured Java code, fields are almost always declared private (see Modifiers) and only made accessible via public getter/setter methods — this is called encapsulation and is one of the four core principles of OOP. It allows the class to later change its internal data representation without requiring code outside the class to be adapted.

Classes vs. instances: a common confusion

Beginners often confuse the class itself with a concrete object: Account is only the blueprint — it exists exactly once in the compiled program. mine and yours are two INDEPENDENT objects created according to this blueprint, each occupying their own memory for their fields.

See also: Objects, Constructors, Methods, OOP, Modifiers