0% found this document useful (0 votes)
2 views3 pages

Java Nested Classes Study Guide FULL FORMAT

Uploaded by

Anif Kma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Java Nested Classes Study Guide FULL FORMAT

Uploaded by

Anif Kma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Nested Classes Study Guide

Complete Revision Guide with Definitions, Types, Tables, Examples, and Explanations

1. What is a Nested Class?


A Nested Class is a class defined inside another class. It helps logically group related classes, improves
encapsulation, and enhances code readability and maintainability.

2. Purpose of Nested Classes

Purpose Explanation

Groups related classes together


Logical grouping

Encapsulation Hides internal implementation

Code organization Cleaner and structured code

Security Restricts unnecessary access

Maintainability Easier debugging and updates

3. Types of Nested Classes in Java

Type Description

Inner Class Non-static class inside another class

Static Nested Class Static class inside another class

Local Inner Class Class inside a method

Anonymous Inner Class Class without name


4. Inner Class
An Inner Class is a non-static class inside another class and can access all members.
class Outer {
private int x = 10;
class Inner {
void display() {
[Link](x);
}
}
}

5. Static Nested Class


A Static Nested Class does not require outer class object and accesses static members only.
class Outer {
static int x = 100;
static class Inner {
void display() {
[Link](x);
}
}
}

6. Local Inner Class


Declared inside method and accessible only within that method.
void show() {
class Inner {
void display() {
[Link]("Local Inner Class");
}
}
}

7. Anonymous Inner Class


Class without name used for one-time implementation.
Animal obj = new Animal() {
void sound() {
[Link]("Dog barks");
}
};
8. Comparison Table

Feature Inner Class Static Nested Local Class Anonymous Class

static keyword No Yes No No

Requires outer Yes No Yes Yes


object

Has name Yes Yes Yes No

Scope Class level Class level Method level Expression level

9. Advantages

Advantage Explanation

Encapsulation Restricts unwanted access

Code organization Cleaner structure

Security Protects internal logic

Maintainability Easy debugging

Logical grouping Better architecture

10. Summary
Nested classes improve structure, security, and maintainability. Java supports four types of nested classes:
Inner, Static Nested, Local, and Anonymous classes.

You might also like