Java के Static Methods
In this page:
static returnType methodName(parameters) {
// body
}
ClassName.methodName(arguments);
What is a Static Method?
एक static method किसी individual object के बजाय class खुद से belong करता है, मतलब आप इसे class का एक single instance बनने से पहले भी call कर सकते हैं।
यह static methods को utility operations के लिए एक natural fit बनाता है जो per-object state पर निर्भर नहीं करते।
उदाहरण: What is a Static Method?
public class Main {
static void sayHello() {
System.out.println("Hello!");
}
public static void main(String[] args) {
sayHello(); // called with no instance created
}
}
Login to try C/C++/Java/PHP code in the editor
Calling Static Methods
आप एक static method class name के through call करते हैं (जैसे Math.max(a, b)), एक object reference के through नहीं — हालांकि Java technically इसे एक instance के through call करने देगी, वह style discouraged है क्योंकि यह यह तथ्य छुपाता है कि असल में कोई instance state उपयोग नहीं हो रहा।
उदाहरण: Calling Static Methods
public class Main {
public static void main(String[] args) {
System.out.println(Math.max(3, 7)); // called through the class name
}
}
Login to try C/C++/Java/PHP code in the editor
Static vs Instance Methods
चूंकि एक static method का refer करने के लिए कोई this नहीं, यह सीधे instance fields पढ़ या लिख नहीं सकता या instance methods call नहीं कर सकता; यह सिर्फ दूसरे static members और parameters के रूप में pass किए किसी भी चीज़ के साथ काम कर सकता है।
एक static method से एक instance field reference करने की कोशिश करना एक compile error है, runtime वाला नहीं।
उदाहरण: Static vs Instance Methods
public class Main {
int instanceValue = 10;
static void staticMethod() {
// System.out.println(instanceValue); // would not compile: no 'this'
System.out.println("Static method running");
}
public static void main(String[] args) {
staticMethod();
}
}
Login to try C/C++/Java/PHP code in the editor
Accessing Variables
Static methods static variables को freely पढ़ और modify कर सकते हैं, जो per object copy होने के बजाय class के हर instance में shared हैं। वह sharing बिल्कुल वही कारण है कि multi-threaded code में static state को carefully उपयोग करना चाहिए।
उदाहरण: Accessing Variables
// Define the class Main
public class Main {
static int counter = 0;
static void increment() {
counter++;
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
increment();
increment();
// Print a line to the console
System.out.println(counter);
}
}
Login to try C/C++/Java/PHP code in the editor
Common Built-in Static Methods
Java की खुद की standard library stateless utility work के लिए static methods पर heavily निर्भर है — Math.sqrt(), Integer.parseInt(), और Arrays.sort() सभी static हैं क्योंकि उनमें से किसी को भी अपना काम करने के लिए एक object की internal state की ज़रूरत नहीं।
उदाहरण: Common Built-in Static Methods
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println(Math.sqrt(16));
// Print a line to the console
System.out.println(Integer.parseInt("42"));
}
}
Login to try C/C++/Java/PHP code in the editor
- एक static method से एक instance method call करना या एक instance variable उपयोग करना, जो एक compile error है।
- एक static method के अंदर
thisउपयोग करना, जो allowed नहीं। - एक object के through एक static method call करना और यह सोचना कि यह उस object का data उपयोग करता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: