0
0
Javaprogramming~15 mins

Static vs non-static behavior in Java

Choose your learning style8 modes available
menu_bookIntroduction

Static and non-static help decide if something belongs to the whole class or just one object. This helps organize code and save memory.

When you want a value or method shared by all objects, like a counter for how many objects exist.
When you want each object to have its own separate data, like a name or age.
When you want to call a method without creating an object, like a utility method to add numbers.
When you want to keep track of something common to all objects, like a setting or configuration.
regular_expressionSyntax
Java
class ClassName {
    static int sharedValue; // static variable
    int individualValue;    // non-static variable

    static void staticMethod() {
        // code here
    }

    void nonStaticMethod() {
        // code here
    }
}

Static means the item belongs to the class itself, not any object.

Non-static means the item belongs to each object separately.

emoji_objectsExamples
line_end_arrow_notchThis class counts how many objects are created using a static variable and shows each object's id using a non-static variable.
Java
class Example {
    static int count = 0; // shared by all objects
    int id;               // unique for each object

    Example(int id) {
        this.id = id;
        count++;
    }

    static void showCount() {
        System.out.println("Total objects: " + count);
    }

    void showId() {
        System.out.println("Object id: " + id);
    }
}
line_end_arrow_notchStatic method is called using the class name. Non-static methods are called on objects.
Java
public class Test {
    public static void main(String[] args) {
        Example e1 = new Example(1);
        Example e2 = new Example(2);

        Example.showCount(); // call static method
        e1.showId();         // call non-static method
        e2.showId();
    }
}
code_blocksSample Program

This program creates two Car objects. It uses a static variable to count all cars made and non-static variables to store each car's model.

Java
class Car {
    static int totalCars = 0; // shared by all cars
    String model;             // unique for each car

    Car(String model) {
        this.model = model;
        totalCars++;
    }

    static void showTotalCars() {
        System.out.println("Total cars made: " + totalCars);
    }

    void showModel() {
        System.out.println("Car model: " + model);
    }
}

public class Main {
    public static void main(String[] args) {
        Car c1 = new Car("Toyota");
        Car c2 = new Car("Honda");

        Car.showTotalCars();
        c1.showModel();
        c2.showModel();
    }
}
OutputSuccess
emoji_objectsImportant Notes
line_end_arrow_notch

Static methods cannot use non-static variables directly because non-static variables belong to objects, and static methods belong to the class.

line_end_arrow_notch

You can call static methods and access static variables without creating an object.

line_end_arrow_notch

Non-static methods can access both static and non-static variables.

list_alt_checkSummary

Static means shared by the whole class, non-static means unique to each object.

Use static for common data or behavior, non-static for individual object data or behavior.

Static methods can be called without objects; non-static methods need objects.