Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · Java · 04 Jul 2026 · Hindi + English

Static Keyword in Java: Variable, Method and Block Examples

static means the member belongs to the class, not to objects. See static variables, methods, blocks, and why main() is static — with runnable examples.

What does static mean?

static means the member belongs to the CLASS, not to individual objects. One shared copy exists no matter how many objects you create — and it can be used without creating any object at all.

Static variable: one shared copy

class Student {
    String name;                 // instance: each object has its own
    static String school = "Alpine Public School";  // shared: ONE copy

    Student(String n) { name = n; }
}

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

        Student.school = "Alpine Khurja";   // change once...
        System.out.println(s1.name + " - " + s1.school);
        System.out.println(s2.name + " - " + s2.school);
    }
}
Aman - Alpine Khurja Priya - Alpine Khurja

Changed once, reflected for every object — because there is only one school variable in memory, stored with the class. Perfect for counters, config values and shared constants.

Static method: call without an object

class MathUtil {
    static int square(int n) {      // no object needed
        return n * n;
    }
}

int r = MathUtil.square(5);          // ClassName.method()
System.out.println(r);
25
The big restriction: a static method cannot directly access instance (non-static) variables or use this — because it runs without any object, so there is no "this object" to refer to. Compile error: "non-static variable cannot be referenced from a static context" — the most Googled Java error by beginners.

Static block: runs once at class loading

class Config {
    static String dbUrl;
    static {                          // runs ONCE when class loads,
        dbUrl = "jdbc:mysql://localhost/school_db";   // before main
        System.out.println("Config loaded");
    }
}

Used for one-time initialization of static data — loading configuration, registering drivers.

Why is main() static? (guaranteed interview question)

Because the JVM must call main() before any object exists. If main were non-static, the JVM would need to create an object of your class first — but with which constructor, with what arguments? Static removes the chicken-and-egg problem: ClassName.main(args) runs directly.

MemberBelongs toAccessCopies
Instance variableEach objectobject.nameOne per object
Static variableClassClassName.nameExactly one
Instance methodObjectobject.method()Uses this
Static methodClassClassName.method()No this

static का मतलब क्या है?

static का मतलब member CLASS का है, individual objects का नहीं. कितने भी objects बनाएं, एक ही shared copy रहती है — और बिना कोई object बनाए भी use हो सकती है.

Static variable: एक shared copy

class Student {
    String name;                 // instance: हर object की अपनी
    static String school = "Alpine Public School";  // shared: EK copy

    Student(String n) { name = n; }
}

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

        Student.school = "Alpine Khurja";   // एक बार बदला...
        System.out.println(s1.name + " - " + s1.school);
        System.out.println(s2.name + " - " + s2.school);
    }
}
Aman - Alpine Khurja Priya - Alpine Khurja

एक बार बदला, हर object में दिखा — क्योंकि memory में school variable एक ही है, class के साथ stored. Counters, config values और shared constants के लिए perfect.

Static method: बिना object के call

class MathUtil {
    static int square(int n) {      // object की ज़रूरत नहीं
        return n * n;
    }
}

int r = MathUtil.square(5);          // ClassName.method()
System.out.println(r);
25
बड़ी restriction: static method directly instance (non-static) variables access नहीं कर सकता और न this use कर सकता है — क्योंकि यह बिना किसी object के चलता है, तो "this object" refer करने को है ही नहीं. Compile error: "non-static variable cannot be referenced from a static context" — beginners का सबसे ज़्यादा Google किया गया Java error.

Static block: class loading पर एक बार चलता है

class Config {
    static String dbUrl;
    static {                          // class load होते ही EK बार,
        dbUrl = "jdbc:mysql://localhost/school_db";   // main से पहले
        System.out.println("Config loaded");
    }
}

Static data की one-time initialization के लिए — configuration load करना, drivers register करना.

main() static क्यों है? (guaranteed interview question)

क्योंकि JVM को main() किसी भी object के बनने से पहले call करना होता है. अगर main non-static होता, तो JVM को पहले आपकी class का object बनाना पड़ता — लेकिन किस constructor से, किन arguments के साथ? Static यह chicken-and-egg problem हटा देता है: ClassName.main(args) directly चलता है.

Memberकिसका हैAccessCopies
Instance variableहर objectobject.nameहर object की एक
Static variableClassClassName.nameExactly एक
Instance methodObjectobject.method()this use करता है
Static methodClassClassName.method()this नहीं

Frequently Asked Questions

What does the static keyword mean in Java?

static makes a member belong to the class itself instead of objects — one shared copy exists and it can be accessed as ClassName.member without creating any object.

Why is the main method static in Java?

So the JVM can call it directly without first creating an object of the class — otherwise there would be a chicken-and-egg problem of which constructor to use.

Can a static method access instance variables?

Not directly. A static method runs without any object, so it has no this reference; it can only access instance members through an explicit object reference.