← Back to Core Java Course | Chapter 7: OOP Core | Lesson 8 of 11

Java की static Keyword

static किसी चीज़ को हर object के बजाय पूरी class से belong करती चिह्नित करता है, wall पर एक shared class rule जैसा। हर object वही एक देखता है।
Syntax
java
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?

java
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
	}
}

Static Methods

Static methods को कभी एक object बनाए बिना सीधे class name के through invoke किया जा सकता है, यही कारण है कि Math.abs() जैसे utility methods static हैं — उनके operate करने के लिए कोई meaningful per-object state नहीं।

उदाहरण: Static Methods

java
public class Main {
	public static void main(String[] args) {
		System.out.println(Math.abs(-5)); // called via class name, no object needed
	}
}

Static Blocks

एक static initializer block बिल्कुल एक बार चलता है, जब JVM class पहली बार load करता है, उस class का कोई भी object बनने से पहले — यह static state setup करने के लिए सही जगह है जो एक simple one-line field initializer के लिए बहुत complex है।

उदाहरण: Static Blocks

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

Static Nested Classes

एक static nested class किसी दूसरी class के अंदर defined है लेकिन outer class के एक instance का implicit reference नहीं रखती, एक regular (non-static) inner class के विपरीत — आप पहले एक outer-class object बनाए बिना एक बना सकते हैं।

उदाहरण: Static Nested Classes

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

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

java
// 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);
	}
}
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 variables उपयोग करना, जो एक compile error है।
  2. हर object से एक static variable की अपनी copy की उम्मीद करना, जब सभी objects एक share करते हैं।
  3. एक static context में this call करना, जो allowed नहीं।

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.