📘 Lesson · Lesson 27
static Keyword
static Keyword
What is static?
The static keyword means a member belongs to the class itself, not to any object. All objects share one copy.
Static Variable and Method
class Counter {
static int count = 0; // shared by all objects
Counter() { count++; }
static void show() { System.out.println("Count: " + count); }
}
new Counter(); new Counter();
Counter.show(); // Count: 2Count: 2
Static Block
A static { } block runs once when the class is first loaded — used for setup.
Summary
- static members belong to the class, shared across all objects.
- Static methods/variables are accessed by ClassName; static block runs once at load.
static क्या है?
static keyword का मतलब member class का खुद का है, किसी object का नहीं। सारे objects एक copy share करते हैं।
Static Variable और Method
class Counter {
static int count = 0; // सभी objects share करते हैं
Counter() { count++; }
static void show() { System.out.println("Count: " + count); }
}
new Counter(); new Counter();
Counter.show(); // Count: 2Count: 2
Static Block
static { } block एक बार चलता है जब class पहली बार load होती है — setup के लिए।
सारांश
- static members class के होते हैं, सभी objects में shared।
- Static methods/variables ClassName से access होते हैं; static block load पर एक बार चलता है।