Java Inner Classes
Keep a closely related helper near its owner.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- Explain the key idea behind Inner Classes.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
A non-static inner class is associated with an enclosing object and can access its members. A static nested class is different: it does not require an enclosing instance. Use nesting when it clarifies the relationship, not just to shorten a file.
விரிவான விளக்கங்கள்
Java Inner Classes என்னன்னு தெரியுமா?
Inner class என்பது ஒரு class-க்கு உள்ளே இருக்குற இன்னொரு class. Outer class-ஓட members-ஐ access பண்றதுக்கும் classes-ஐ group பண்றதுக்கும் use ஆகுது.
Inner Class Access
Outer class-ஓட object create பண்ணி, பிறகு inner class-ஐ access பண்ணலாம்:
Static Inner Class
Static inner class-ஐ outer class-ஓட object இல்லாமலே access பண்ணலாம்:
class OuterClass {
int x = 10;
class InnerClass {
void show() {
System.out.println("Outer x = " + x);
}
}
static class StaticInner {
void greet() {
System.out.println("Hello from static inner!");
}
}
}
OuterClass outer = new OuterClass();
OuterClass.InnerClass inner = outer.new InnerClass();
inner.show(); // Outer x = 10
OuterClass.StaticInner si = new OuterClass.StaticInner();
si.greet(); // Hello from static inner!Anonymous inner class எப்போது பயன்படுத்துவோம்?
- Large complex classes-க்கு
- ஒரு interface/abstract class-ஐ ஒரே இடத்தில் use செய்ய, தனியே class எழுதாமல்
- Static methods-க்கு
- Multiple inheritance-க்கு
Reveal answer / விடை
ஒரு interface/abstract class-ஐ ஒரே இடத்தில் use செய்ய, தனியே class எழுதாமல்
Anonymous inner class ஒரே இடத்தில் use ஆகும் போது தனியே class எழுதாமல் inline-ல் define செய்யும். Java 8+ Lambda expressions இதை மேலும் simple-ஆக்கின.
முழுமையான உதாரணம்
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
public class Main {
public static void main(String[] args) throws Exception {
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.show();
}
}
class Outer {
int value = 10;
class Inner { void show() { System.out.println("Outer value: " + value); } }
}Predict first. Then reveal the expected output / வெளியீடு
Outer value: 10
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
நீங்களே முயற்சி செய்யுங்கள்
Practice challenge
Change the outer field value before calling inner.show().
Reveal the answer and reasoning / விடை
The inner object reads the field through its enclosing Outer object, so it sees the updated value.
தவிர்க்க வேண்டிய தவறு: A non-static inner class requires an enclosing instance. new Outer.Inner() is not the correct construction form for this example.
பாடத்தின் சுருக்கம்
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.