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

Java की Inner Classes

एक inner class एक class है जो किसी दूसरी class के अंदर रखी गई है, एक बड़े cabinet के अंदर बनी एक छोटी drawer जैसी। यह अपनी outer class में पहुंच सकती है।
Syntax
java
class OuterClass {
    class InnerClass {
        // inner members
    }
}

OuterClass.InnerClass inner = outer.new InnerClass();

What is an Inner Class?

एक inner class पूरी तरह किसी दूसरी (outer) class के अंदर defined है और आमतौर पर उस outer class की ज़रूरतों को closely serve करने के लिए मौजूद है — यह एक छोटी helper class को उसी बड़ी class के साथ logically group करने का एक तरीका है जो इसे असल में उपयोग करती है।

उदाहरण: What is an Inner Class?

java
// Define the class Outer
class Outer {
	// Define the class Inner
	class Inner {
		void show() {
			// Print a line to the console
			System.out.println("Inside inner class");
		}
	}
}
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new Outer object and store it in outer
		Outer outer = new Outer();
		Outer.Inner inner = outer.new Inner();
		inner.show();
	}
}

Instantiating Inner Classes

एक regular (non-static) inner class का instance बनाने के लिए पहले एक मौजूदा outer-class instance चाहिए, क्योंकि inner object implicitly इससे बंधा है — syntax outerInstance.new InnerClass() जैसा दिखता है।

उदाहरण: Instantiating Inner Classes

java
class Outer {
	class Inner {
	}
}
public class Main {
	public static void main(String[] args) {
		Outer outer = new Outer();
		Outer.Inner inner = outer.new Inner(); // requires an outer instance first
		System.out.println(inner);
	}
}

Accessing Outer Class Members

एक non-static inner class सीधे अपनी enclosing outer class के हर member को access कर सकती है, private fields और methods सहित, बिना किसी special access modifiers या getter/setter workaround के — यह tight coupling बिल्कुल वह है जो inner classes को closely related helper logic के लिए convenient बनाता है।

उदाहरण: Accessing Outer Class Members

java
class Outer {
	private int value = 42;
	class Inner {
		void show() {
			System.out.println(value); // reaches outer's private field directly
		}
	}
}
public class Main {
	public static void main(String[] args) {
		Outer outer = new Outer();
		outer.new Inner().show();
	}
}

Static Nested Classes

एक static nested class, static keyword से declared, किसी outer-class instance से कोई implicit link नहीं रखती, इसलिए आप इसे पहले एक Outer object बनाए बिना अकेले बना सकते हैं (new Outer.StaticNested())।

उदाहरण: Static Nested Classes

java
class Outer {
	static class Nested {
		void show() {
			System.out.println("Static nested class");
		}
	}
}
public class Main {
	public static void main(String[] args) {
		Outer.Nested nested = new Outer.Nested(); // no outer instance needed
		nested.show();
	}
}

Local Inner Classes

एक local inner class class level पर नहीं बल्कि एक method body के अंदर declared है, और इसका scope उस method तक सीमित है — यह एक niche tool है एक helper class के लिए जिसे सिर्फ एक specific logic के टुकड़े के अंदर चाहिए होता है।

उदाहरण: Local Inner Classes

java
public class Main {
	static void method() {
		class Helper { // scoped to this method only
			void show() {
				System.out.println("Local inner class");
			}
		}
		new Helper().show();
	}
	public static void main(String[] args) {
		method();
	}
}
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. Outer instance के बिना एक inner class object बनाना, जैसे static code से new Inner(), जो एक compile error है।
  2. main से एक non-static inner class उपयोग करना बिना एक outer object के।
  3. एक inner class को एक static nested class के साथ confuse करना, क्योंकि एक static nested class को एक outer object की ज़रूरत नहीं।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

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.