Lesson 36 of 50 – Java static Keyword
72%

Java static Keyword

The static keyword in Java is used to define members that belong to the class rather than to individual objects. It can be used with variables, methods, initializer blocks, and nested classes.

1. What Is the static Keyword?

A static member is associated with the class itself. You can usually access a static field or method using the class name.

class Student {
    static String college = "ABC College";

    public static void main(String[] args) {
        System.out.println(Student.college);
    }
}
Output:
ABC College

Here, college is a static variable associated with the Student class.

2. Static Variables

A static variable is shared by all instances of its class. There is one class-level field rather than a separate copy for each object.

class Student {
    String name;
    static String college = "ABC College";

    Student(String name) {
        this.name = name;
    }

    void display() {
        System.out.println(name + " - " + college);
    }

    public static void main(String[] args) {
        Student s1 = new Student("Rahul");
        Student s2 = new Student("Priya");

        s1.display();
        s2.display();
    }
}
Output:
Rahul - ABC College
Priya - ABC College
Use static variables for data that should be shared across instances, such as a college name or a common configuration value.
3. Static Methods

A static method belongs to the class. It can be called without creating an instance of that class.

class Calculator {

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

    public static void main(String[] args) {
        int result = Calculator.add(10, 20);

        System.out.println(result);
    }
}
Output:
30

The method add() is called using the class name, Calculator.

4. Static vs Instance Variables
Static Variable Instance Variable
Associated with the class. Associated with each object.
One shared class-level field. Each object has its own field.
Can be accessed using the class name. Normally accessed through an object.
class Demo {
    static int count = 0;
    int number = 0;

    public static void main(String[] args) {
        Demo d1 = new Demo();
        Demo d2 = new Demo();

        d1.count++;
        d1.number++;

        d2.count++;
        d2.number++;

        System.out.println(Demo.count);
        System.out.println(d1.number);
        System.out.println(d2.number);
    }
}
Output:
2
1
1

The static field is shared, while each object has its own instance field.

5. Static Methods Cannot Directly Access Instance Members

A static method does not have an automatic reference to a particular object. Therefore, it cannot directly access instance variables or instance methods.

class Demo {
    int number = 10;

    static void show() {
        // System.out.println(number);
        // Compilation error
    }

    public static void main(String[] args) {
        Demo d1 = new Demo();

        System.out.println(d1.number);
    }
}
Output:
10

To access the instance field inside a static method, use an object reference such as d1.number.

6. The main() Method Is Static

The Java entry-point method is commonly declared public static void main(String[] args). The static modifier allows the runtime to invoke this entry point without first creating an instance of the class.

class Demo {

    public static void main(String[] args) {
        System.out.println("Java Program Started");
    }
}
Output:
Java Program Started

Meaning of the declaration:

  • public — accessible to the launcher.
  • static — associated with the class.
  • void — returns no value.
  • main — entry-point method name.
  • String[] args — receives command-line arguments.
7. Static Initialization Block

A static initialization block is used to initialize static data. It runs when the class is initialized, before its first active use that triggers initialization.

class Demo {
    static int number;

    static {
        number = 100;
        System.out.println("Static block executed");
    }

    public static void main(String[] args) {
        System.out.println(number);
    }
}
Output:
Static block executed
100

The static block assigns a value to the static field before the main method prints it.

8. Static Methods and Object Creation

A static method can create an object and use its instance members through that object's reference.

class Student {
    String name = "Rahul";

    void display() {
        System.out.println(name);
    }

    static void showStudent() {
        Student s1 = new Student();
        s1.display();
    }

    public static void main(String[] args) {
        Student.showStudent();
    }
}
Output:
Rahul

The static method creates a Student object and calls its instance method through that object.

9. Static Members and Inheritance

Static methods can be inherited by subclasses when access rules allow it. A subclass can also declare a static method with the same signature, which hides the superclass method rather than overriding it.

class Parent {
    static void show() {
        System.out.println("Parent static method");
    }
}

class Child extends Parent {
    static void show() {
        System.out.println("Child static method");
    }

    public static void main(String[] args) {
        Parent.show();
        Child.show();
    }
}
Output:
Parent static method
Child static method
Static methods use method hiding, not runtime overriding. Their selection depends on the class-qualified call or the compile-time reference type.
10. Static Nested Classes

Java allows a static nested class to be declared inside another class. Unlike a non-static inner class, it does not require an instance of the enclosing class to be created.

class Outer {

    static class Inner {
        void display() {
            System.out.println("Static nested class");
        }
    }

    public static void main(String[] args) {
        Outer.Inner obj = new Outer.Inner();
        obj.display();
    }
}
Output:
Static nested class

The nested class is accessed using Outer.Inner.

11. Common Mistakes with static

Mistake 1: Using this in a static method

static void show() {
    // this.display(); // Compilation error
}

Mistake 2: Accessing an instance field directly

class Demo {
    int number = 10;

    static void show() {
        // System.out.println(number);
        // Compilation error
    }
}

Mistake 3: Treating a shared field as object-specific

class Counter {
    static int count = 0;
}

Changing Counter.count affects the single shared static field, not a separate copy belonging to each object.

12. Real-Life Example: Student Counter
class Student {
    String name;
    static int totalStudents = 0;

    Student(String name) {
        this.name = name;
        totalStudents++;
    }

    void display() {
        System.out.println("Name: " + name);
    }

    public static void main(String[] args) {
        Student s1 = new Student("Rahul");
        Student s2 = new Student("Priya");
        Student s3 = new Student("Amit");

        s1.display();
        s2.display();
        s3.display();

        System.out.println(
            "Total Students: " + Student.totalStudents
        );
    }
}
Output:
Name: Rahul
Name: Priya
Name: Amit
Total Students: 3

The static variable counts how many Student objects have been constructed during this program's execution.

13. Practice Assignments
  1. Create a class with a static variable named collegeName and print it using the class name.
  2. Create a static method named add() that returns the sum of two integers.
  3. Create a Student class with an instance variable for name and a static variable for college.
  4. Create a static initialization block that assigns a value to a static integer.
  5. Create a class that counts the number of objects created using a static counter.
  6. Create a static method that creates an object and calls one of its instance methods.
  7. Create Parent and Child classes to demonstrate static method hiding.
Practice Tip: Compare static and instance members by creating multiple objects and observing which values are shared.

🧠 Quick Quiz

1. What does a static variable belong to?




2. Can a static method directly use this?




3. What is a static initialization block used for?