Static

Static — makes a field, method, or class variable belong to the class itself, rather than to instances of the class.

public class Car {
    private static int carCount = 0;

    public Car() {
        carCount++; // Increment the static car count for every new Car created
    }

    // Static method
    public static int getCarCount() {
        return carCount;
    }
}
Car car1 = new Car();
Car car2 = new Car();
System.out.println(Car.getCarCount()); // Outputs: 2

Static members are shared across all instances of the class, allowing for values or functions common to every object.

Static — Structure map

Clickable & Draggable!

Static — Related pages: