Java की Inner Classes
In this page:
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?
// 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();
}
}
Login to try C/C++/Java/PHP code in the editor
Instantiating Inner Classes
एक regular (non-static) inner class का instance बनाने के लिए पहले एक मौजूदा outer-class instance चाहिए, क्योंकि inner object implicitly इससे बंधा है — syntax outerInstance.new InnerClass() जैसा दिखता है।
उदाहरण: Instantiating Inner Classes
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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();
}
}
Login to try C/C++/Java/PHP code in the editor
Static Nested Classes
एक static nested class, static keyword से declared, किसी outer-class instance से कोई implicit link नहीं रखती, इसलिए आप इसे पहले एक Outer object बनाए बिना अकेले बना सकते हैं (new Outer.StaticNested())।
उदाहरण: Static Nested Classes
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();
}
}
Login to try C/C++/Java/PHP code in the editor
Local Inner Classes
एक local inner class class level पर नहीं बल्कि एक method body के अंदर declared है, और इसका scope उस method तक सीमित है — यह एक niche tool है एक helper class के लिए जिसे सिर्फ एक specific logic के टुकड़े के अंदर चाहिए होता है।
उदाहरण: Local Inner Classes
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();
}
}
Login to try C/C++/Java/PHP code in the editor
- Outer instance के बिना एक inner class object बनाना, जैसे static code से
new Inner(), जो एक compile error है। mainसे एक non-static inner class उपयोग करना बिना एक outer object के।- एक 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: