Β 

πŸ”Ή static Keyword in JavaΒ 

In Java, the static keyword is like a shared treasure chest πŸ—οΈ β€” once created, it belongs to the class itself, not to individual objects.

πŸ‘‰ This means all objects can access it without creating multiple copies.


✨ What Does static Mean?

  • Non-static members β†’ belong to objects (every object gets its own copy).

  • Static members β†’ belong to the class (shared by all objects).

Think of it like a school notice board 🏫 – everyone can read it, but it’s placed only once for the whole class.


πŸ›  Uses of static Keyword in Java

1️⃣ Static Variables (Class Variables)

  • Shared across all objects.

  • Only one copy exists, no matter how many objects are created.

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

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

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

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("Ravi");
        Student s2 = new Student("Anita");

        s1.display();
        s2.display();
    }
}

βœ… Output:

Ravi - ABC College
Anita - ABC College

πŸ‘‰ Notice: College name is shared by all students!


2️⃣ Static Methods

  • Can be called without creating objects.

  • Can only access static variables (no instance variables directly).

class MathUtil {
    static int square(int n) {
        return n * n;
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(MathUtil.square(5)); // No object needed
    }
}

βœ… Output:

25

3️⃣ Static Blocks

  • Used to initialize static variables.

  • Runs only once when the class is loaded into memory.

class Example {
    static int num;

    static {
        num = 10;
        System.out.println("Static block executed!");
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println("Number: " + Example.num);
    }
}

βœ… Output:

Static block executed!
Number: 10

4️⃣ Static Classes (Nested Classes)

  • Java doesn’t allow top-level static classes,
    but nested classes can be made static.

class Outer {
    static class Inner {
        void show() {
            System.out.println("Static Nested Class");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Outer.Inner obj = new Outer.Inner();
        obj.show();
    }
}

πŸš€ Quick Recap

  • Static Variable β†’ Shared across all objects.

  • Static Method β†’ Can be called without objects.

  • Static Block β†’ Runs once when class is loaded.

  • Static Class β†’ Nested class that can exist without outer object.

🎯 Why Use static?

βœ” Saves memory 🧠 (only one copy).
βœ” Useful for utility methods (like Math.sqrt()).
βœ” Makes code faster to access.

πŸ’‘ Interview Tip: A common question is β€”
πŸ‘‰ β€œCan a static method access non-static variables?”
Answer: ❌ No, because non-static variables belong to objects, and static methods don’t need objects!

Β