Java Inner Classes — தமிழ்

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 34 / 50 · தமிழ்

Java Inner Classes

Keep a closely related helper near its owner.

Step-by-step explanationsRunnable JavaPractice + answer

முக்கிய விளக்கங்கள் தமிழில் உள்ளன. 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.
01 / The concept

அடிப்படையைப் புரிந்துகொள்ளுங்கள்

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.

02 / Step by step

விரிவான விளக்கங்கள்

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 பண்ணலாம்:

Java reference fragmentJAVA
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 எப்போது பயன்படுத்துவோம்?

  1. Large complex classes-க்கு
  2. ஒரு interface/abstract class-ஐ ஒரே இடத்தில் use செய்ய, தனியே class எழுதாமல்
  3. Static methods-க்கு
  4. Multiple inheritance-க்கு
Reveal answer / விடை

ஒரு interface/abstract class-ஐ ஒரே இடத்தில் use செய்ய, தனியே class எழுதாமல்

Anonymous inner class ஒரே இடத்தில் use ஆகும் போது தனியே class எழுதாமல் inline-ல் define செய்யும். Java 8+ Lambda expressions இதை மேலும் simple-ஆக்கின.

03 / Read and predict

முழுமையான உதாரணம்

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.

Main.java · complete runnable exampleJAVA
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.

04 / Learn by changing

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.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

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.

05 / Your turn

நீங்களே முயற்சி செய்யுங்கள்

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.

Coding Tamilan · Understand the idea. Try the code. Build the habit.