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 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);
}
}
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);
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.
| Member | Belongs to | Access | Copies |
|---|---|---|---|
| Instance variable | Each object | object.name | One per object |
| Static variable | Class | ClassName.name | Exactly one |
| Instance method | Object | object.method() | Uses this |
| Static method | Class | ClassName.method() | No this |
static का मतलब क्या है?
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);
}
}
एक बार बदला, हर 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);
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 | किसका है | Access | Copies |
|---|---|---|---|
| Instance variable | हर object | object.name | हर object की एक |
| Static variable | Class | ClassName.name | Exactly एक |
| Instance method | Object | object.method() | this use करता है |
| Static method | Class | ClassName.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.