Java Inner Classes
Keep a closely related helper near its owner.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind Inner Classes.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
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.
Explore the details
Java Inner Classes
In Java, it is also possible to nest classes (a class within a class). The purpose of nested classes is to group classes that belong together, which makes your code more readable and maintainable.
To access the inner class, create an object of the outer class, and then create an object of the inner class:
Private Inner Class
Unlike a "regular" class, an inner class can be private or protected.
If you don't want outside objects to access the inner class, declare
the class as private:
Static Inner Class
An inner class can also be static, which means that you can access it without
creating an object of the outer class:
Note: just like static attributes and methods, a static inner class does not have access to members of the outer class.
Access Outer Class From Inner Class
class OuterClass {
int x = 10;
class InnerClass {
void show() {
// Inner class accesses outer class field
System.out.println("Outer x = " + x);
}
}
static class StaticInner {
void greet() {
System.out.println("Hello from static inner class!");
}
}
}
// Regular inner class (needs outer instance)
OuterClass outer = new OuterClass();
OuterClass.InnerClass inner = outer.new InnerClass();
inner.show(); // Outer x = 10
// Static inner class (no outer instance needed)
OuterClass.StaticInner si = new OuterClass.StaticInner();
si.greet(); // Hello from static inner class!One advantage of inner classes, is that they can access attributes and methods of the outer class:
A complete runnable example
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.
Make it your own
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.
One mistake to avoid: A non-static inner class requires an enclosing instance. new Outer.Inner() is not the correct construction form for this example.
Your learning checkpoint
- 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.