← Back to Core Java Course | Chapter 5: Methods & Arrays | Lesson 7 of 10

Java के Static Methods

एक static method किसी एक object के बजाय class खुद से belong करता है, कोई भी बिना ownership के उपयोग कर सकने वाला public water fountain जैसा। आप इसे class name उपयोग करके call करते हैं।
Syntax
java
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?

java
public class Main {
	static void sayHello() {
		System.out.println("Hello!");
	}
	public static void main(String[] args) {
		sayHello(); // called with no instance created
	}
}

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

java
public class Main {
	public static void main(String[] args) {
		System.out.println(Math.max(3, 7)); // called through the class name
	}
}

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

java
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();
	}
}

Accessing Variables

Static methods static variables को freely पढ़ और modify कर सकते हैं, जो per object copy होने के बजाय class के हर instance में shared हैं। वह sharing बिल्कुल वही कारण है कि multi-threaded code में static state को carefully उपयोग करना चाहिए।

उदाहरण: Accessing Variables

java
// 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);
	}
}

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

java
// 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"));
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक static method से एक instance method call करना या एक instance variable उपयोग करना, जो एक compile error है।
  2. एक static method के अंदर this उपयोग करना, जो allowed नहीं।
  3. एक object के through एक static method call करना और यह सोचना कि यह उस object का data उपयोग करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.