Java Lesson 27 – Static | Dataplexa

Static Keyword

The static keyword in Java is used to define members that belong to the class itself rather than to individual objects. Understanding static is essential for writing efficient, clean, and well-structured Java programs.

In real-world Java applications, the static keyword is used frequently for shared data, utility methods, constants, and application-level logic.


Real-World Meaning of Static

Think about a company where all employees follow the same company policy. The policy is shared across everyone and does not belong to a single employee.

Similarly, static members belong to the class and are shared by all objects created from that class.


Static Variables

A static variable is shared among all instances of a class. Only one copy exists in memory, regardless of how many objects are created.


class Employee {

    static String companyName = "Dataplexa";
    String employeeName;

    Employee(String name) {
        employeeName = name;
    }

    void display() {
        System.out.println(employeeName + " works at " + companyName);
    }
}

Here, companyName is shared by all employees, while employeeName is unique to each object.


Using Static Variables


public class Main {
    public static void main(String[] args) {

        Employee e1 = new Employee("Alice");
        Employee e2 = new Employee("Bob");

        e1.display();
        e2.display();
    }
}

Both employees refer to the same company name stored at the class level.


Static Methods

A static method belongs to the class and can be called without creating an object. It cannot directly access non-static variables or methods.


class Calculator {

    static int add(int a, int b) {
        return a + b;
    }
}

Static methods are commonly used for utility operations.


Calling Static Methods

Static methods are accessed using the class name.


public class Main {
    public static void main(String[] args) {

        int result = Calculator.add(10, 20);
        System.out.println(result);
    }
}

No object creation is required to use static methods.


Static Block

A static block is used to initialize static data. It executes once when the class is loaded into memory.


class AppConfig {

    static {
        System.out.println("Application configuration loaded");
    }
}

Static blocks are useful for one-time setup tasks.


Why Static Members Are Useful

  • They reduce memory usage
  • They represent shared behavior or data
  • They improve performance
  • They simplify utility class design

Most Java frameworks use static constants and utility methods internally.


Common Mistakes with Static

  • Trying to access instance variables inside static methods
  • Overusing static, making code hard to test
  • Using static where object behavior is required

Static should be used carefully and intentionally.


Key Takeaways

  • Static members belong to the class, not objects
  • Static variables are shared across instances
  • Static methods can be called without creating objects
  • Static blocks run once when the class loads

In the next lesson, we will explore the final keyword and understand how it enforces immutability in Java.