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

Java में Static Keyword: Variable, Method और Block Examples

static का मतलब member class का है, objects का नहीं. Static variables, methods, blocks और main() static क्यों है — 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

Java में static keyword का क्या मतलब है?

static member को objects की बजाय class का बना देता है — एक shared copy रहती है और बिना object बनाए ClassName.member से access होती है.

Java में main method static क्यों है?

ताकि JVM उसे class का object बनाए बिना directly call कर सके — वरना कौन-सा constructor use हो, यह chicken-and-egg problem बन जाती.

क्या static method instance variables access कर सकता है?

Directly नहीं. Static method बिना object के चलता है, इसलिए उसके पास this reference नहीं होता; instance members सिर्फ explicit object reference से access हो सकते हैं.