Inner Classes in Java

 

🔷 What is an Inner Class?

An Inner Class is a class defined inside another class. Java allows nesting classes to logically group classes that are only used in one place, increasing encapsulation and readability.


✅ Why Use Inner Classes?

  • Logical grouping of classes

  • Better encapsulation

  • Can access members (even private ones) of the outer class

  • Useful in event handling, GUI applications, etc.


🔰 Types of Inner Classes in Java:

TypeDescription
1. Member Inner ClassRegular class defined within another class.
2. Static Nested ClassStatic class defined inside another class. Doesn’t need outer class object.
3. Local Inner ClassDefined inside a method or block.
4. Anonymous Inner ClassA class without a name, used for one-time use (like in GUI event handling).

🔷 1. Member Inner Class (Non-static)

📌 Example:

class Outer { int outerVar = 100; class Inner { void show() { System.out.println("Outer variable: " + outerVar); } } } public class Main { public static void main(String[] args) { Outer outer = new Outer(); Outer.Inner inner = outer.new Inner(); // Create inner object inner.show(); } }

🔷 2. Static Nested Class

  • Can only access static members of the outer class.

  • Does not need an instance of the outer class to be created.

📌 Example:

class Outer {
static int data = 30; static class StaticInner { void display() { System.out.println("Data: " + data); } } } public class Main { public static void main(String[] args) { Outer.StaticInner obj = new Outer.StaticInner(); obj.display(); } }

🔷 3. Local Inner Class

  • Defined inside a method.

  • Only accessible within that method.

  • Can access final or effectively final variables of the method.

📌 Example:


class Outer { void outerMethod() { int num = 50; // effectively final class LocalInner { void display() { System.out.println("Number: " + num); } } LocalInner inner = new LocalInner(); inner.display(); } } public class Main { public static void main(String[] args) { Outer outer = new Outer(); outer.outerMethod(); } }

🔷 4. Anonymous Inner Class

  • A class with no name.

  • Used to implement an interface or extend a class on the fly.

  • Commonly used in GUI or event handling.

📌 Example:


abstract class Greeting { abstract void sayHello(); } public class Main { public static void main(String[] args) { Greeting g = new Greeting() { void sayHello() { System.out.println("Hello from anonymous class!"); } }; g.sayHello(); } }

🧠 Summary Table

Type of Inner ClassAccess to Outer         ClassStatic?Common Use Case
Member Inner Class    YesNo    Accessing outer class instance members
Static Nested Class        Only staticYesUtility grouping
Local Inner Class    Yes (final vars only)NoTemporary use inside method
Anonymous Inner Class    YesNoEvent handling / one-time use

Comments

Popular posts from this blog

Object Oriented Programming PBCST304 KTU BTech CS Semester 3 2024 Scheme - Dr Binu V P

Introduction to Java Programming

Java Compiler and Java Virtual Machine