Using static Modifier for Class-Level Variables and Methods in Java

In Java, static means that a specific member (a variable, method, nested class, or initializer block) is associated with the class itself rather than its instances. To elaborate: Static Variables: These are tied to the class, not its instances. Initialized just once when the execution begins, they reside in the static memory, ensuring a single shared copy among all class instances. Static Methods: On the class rather than any instance, such methods can only interact with static data members and invoke other static methods. They cannot access or interact with instance-specific data or methods. Static Class: Exclusively, nested classes can be static. Without needing an outer class instance, one can access static nested classes. Static Initialization Block: Employed for initializing static variables, this block runs once upon class loading into memory. In this Java Static Example, we showed the work of various static elements. Click Execute to run the Java Static Example online and see the result.
Using static Modifier for Class-Level Variables and Methods in Java Execute
public class StaticExample {
    static int counter = 0;

    public StaticExample() {

    static void showCounter() {
        System.out.println("Number of instances created: " + counter);

    public static void main(String[] args) {
        StaticExample obj1 = new StaticExample();
        StaticExample obj2 = new StaticExample();
Updated: Viewed: 37 times