Java की static Keyword
In this page:
static dataType field;
static returnType methodName() { }
ClassName.field;
ClassName.methodName();
What is the static Keyword?
static keyword एक member को किसी individual object के बजाय class खुद से बांधता है, इसलिए एक static variable memory में बिल्कुल एक जगह मौजूद है और उस class के हर instance से shared है बजाय per object copy होने के।
उदाहरण: What is the static Keyword?
class Counter {
static int total = 0;
}
public class Main {
public static void main(String[] args) {
Counter.total++;
Counter c = new Counter();
c.total++;
System.out.println(Counter.total); // shared by all instances
}
}
Login to try C/C++/Java/PHP code in the editor
Static Methods
Static methods को कभी एक object बनाए बिना सीधे class name के through invoke किया जा सकता है, यही कारण है कि Math.abs() जैसे utility methods static हैं — उनके operate करने के लिए कोई meaningful per-object state नहीं।
उदाहरण: Static Methods
public class Main {
public static void main(String[] args) {
System.out.println(Math.abs(-5)); // called via class name, no object needed
}
}
Login to try C/C++/Java/PHP code in the editor
Static Blocks
एक static initializer block बिल्कुल एक बार चलता है, जब JVM class पहली बार load करता है, उस class का कोई भी object बनने से पहले — यह static state setup करने के लिए सही जगह है जो एक simple one-line field initializer के लिए बहुत complex है।
उदाहरण: Static Blocks
class Config {
static int value;
static {
value = 42; // runs once, when the class loads
}
}
public class Main {
public static void main(String[] args) {
System.out.println(Config.value);
}
}
Login to try C/C++/Java/PHP code in the editor
Static Nested Classes
एक static nested class किसी दूसरी class के अंदर defined है लेकिन outer class के एक instance का implicit reference नहीं रखती, एक regular (non-static) inner class के विपरीत — आप पहले एक outer-class object बनाए बिना एक बना सकते हैं।
उदाहरण: Static Nested Classes
// Define the class Outer
class Outer {
// Define the class Nested
static class Nested {
void show() {
// Print a line to the console
System.out.println("Inside static nested class");
}
}
}
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
Outer.Nested nested = new Outer.Nested();
nested.show();
}
}
Login to try C/C++/Java/PHP code in the editor
Static vs Instance Elements
Instance variables individually हर object से belong करते हैं, जबकि static variables class-wide shared हैं; instance methods दोनों तरह को freely पढ़ और लिख सकते हैं, लेकिन static methods सिर्फ static members तक restricted हैं क्योंकि उनके पास काम करने के लिए कोई specific object context नहीं।
उदाहरण: Static vs Instance Elements
// Define the class Demo
class Demo {
static int staticVar = 0;
// Declare instanceVar and set it to 0
int instanceVar = 0;
static void staticMethod() {
staticVar++;
}
void instanceMethod() {
staticVar++;
instanceVar++;
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
Demo.staticMethod();
// Create a new Demo object and store it in d
Demo d = new Demo();
d.instanceMethod();
// Print a line to the console
System.out.println(Demo.staticVar);
}
}
Login to try C/C++/Java/PHP code in the editor
- एक static method के अंदर instance variables उपयोग करना, जो एक compile error है।
- हर object से एक
staticvariable की अपनी copy की उम्मीद करना, जब सभी objects एक share करते हैं। - एक static context में
thiscall करना, जो allowed नहीं।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: