Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java Static Explained: Variables, Methods, and Blocks – Hands‑On Examples

In Java, the static keyword appears in three distinct contexts: static variables, static methods, and static blocks. Each plays a unique role in the lifecycle of a class and its instances.

Static Variables in Java

A static variable is a class-level field that is initialized only once when the class is first loaded. Unlike instance variables, a single copy of a static variable is shared by all objects of that class.

Syntax:

public static int count;

Static Methods in Java

Static methods belong to the class rather than to any particular instance. They can be invoked without creating an object and can only access static data.

All main methods are static so that the JVM can launch an application before any objects exist.

Practical Example: Using Static Variables and Methods

Below is a simple program that demonstrates how a static counter is shared across multiple Student objects.

public class Demo {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.showData();
        Student s2 = new Student();
        s2.showData();
        // Student.b++; // uncomment to modify the counter
        // s1.showData();
    }
}

class Student {
    int a;          // instance variable, defaults to 0
    static int b;   // static counter, initialized to 0 once

    Student() {
        b++; // increment counter each time a Student is created
    }

    public void showData() {
        System.out.println("Value of a = " + a);
        System.out.println("Value of b = " + b);
    }
}

Compile and run with java Demo to see the following output:

Java Static Explained: Variables, Methods, and Blocks – Hands‑On Examples

The diagram below illustrates how reference variables and objects are created, and how the static variable b is shared across instances.

Java Static Explained: Variables, Methods, and Blocks – Hands‑On Examples

To access the static variable from outside the class, use ClassName.variableName:

Student.b++; // increments the shared counter
s1.showData(); // will show updated value of b

Attempting to access an instance variable from a static context results in a compilation error:

error: non-static variable a cannot be referenced from a static context
    a++;

This happens because static methods cannot directly interact with instance fields.

Static Blocks in Java

A static block is a special block of code that executes once when the class is first loaded into the JVM. It’s ideal for initializing static fields that require more than a simple assignment.

class Test {
    static int a;
    static int b;

    static {
        a = 10;
        b = 20;
    }
}

Running the following program demonstrates the output of a static block:

public class Demo {
    static int a;
    static int b;

    static {
        a = 10;
        b = 20;
    }

    public static void main(String[] args) {
        System.out.println("Value of a = " + a);
        System.out.println("Value of b = " + b);
    }
}

Output:

Value of a = 10
Value of b = 20

Java

  1. Java Methods: How to Define, Call, and Use Them Effectively
  2. Mastering Java Polymorphism: Concepts, Examples, and Best Practices
  3. Understanding Java Nested Static Classes: Usage, Differences, and Examples
  4. Mastering Java Anonymous Inner Classes: Definition, Syntax, and Practical Examples
  5. Mastering Java’s ObjectInputStream: A Comprehensive Guide
  6. Mastering Abstraction in OOP: Java Abstract Classes & Methods Explained
  7. Polymorphism in Java: A Comprehensive Guide with Practical Examples
  8. Java Abstraction: Mastering Abstract Classes, Methods, and Practical Examples
  9. Java Interfaces Explained: How to Define and Implement Them with Practical Examples
  10. Interface vs Abstract Class in Java: How to Choose the Right Abstraction