SlideShare a Scribd company logo
Nested and Inner
Classes in Java
Introduction
 Java allows classes to be defined within other classes.
 These are called Nested Classes.
 They help in logically grouping classes and controlling visibility.
 Nested classes can access out class members and improve code modularity
Types of Nested Classes
1. Static Nested Class
2. Non-static Nested Class (Inner Class)
- Member Inner Class
- Local Inner Class
- Anonymous Inner Class
Static Nested Class - Basic Example
class Outer {
static int a = 10;
static class StaticNested {
void show() {
System.out.println("Static a: " + a);
}
}
}
Usage:
 Outer.StaticNested obj = new Outer.StaticNested();
 obj.show();
•Defined using static keyword inside another class
•Can access only static members of the outer class.
Static Nested Class -
Advanced Example
class MathUtil {
static class Calculator {
static int square(int x) {
return x * x;
}
}
}
 Usage:
int result = MathUtil.Calculator.square(5);
System.out.println(result); // 25
Static Nested Class - Benefits
• Does not require access to outer class instance.
• Useful for grouping static helpers.
• Static nested classes allow you to organize related
static methods and constants within a containing class.
• This helps keep your code more structured and modular,
especially for tools, calculators, or common operations.
• Ideal for utility classes — classes that provide
commonly used static methods.
Inner Class - Basic Example
class Outer {
int a = 10;
class Inner {
void display() {
System.out.println("a: " + a);
}
}
}
 Usage:
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.display();
•Non-static class defined inside another class.
•Can access both static and non-static members of the outer class.
•Needs an object of the outer class to be created.
Inner Class - Advanced Example
class Bank {
private String bankName = "ABC Bank";
class Account {
void display() {
System.out.println("Welcome to " + bankName);
}
}
}
 Usage:
Bank bank = new Bank();
Bank.Account acc = bank.new Account();
acc.display();
Advantages:
• Inner class has full access to outer class members.
• Ideal when the inner class depends heavily on the outer class for its functionality.
Local Inner Class
class Outer {
void outerMethod() {
class LocalInner {
void msg() {
System.out.println("Inside Local Inner Class");
}
}
LocalInner obj = new LocalInner();
obj.msg();
}
}
Key Points:
•Defined inside a method.
•Can access final or effectively final variables from method.
Anonymous Inner Class
abstract class Animal {
abstract void sound();
}
class Test {
public static void main(String args[]) {
Animal dog = new Animal() {
void sound() {
System.out.println("Woof Woof");
}
};
dog.sound();
}
}
When to use:
• When creating an instance of class with certain methods overridden.
• We need to redefining or implementing a method immediately while creating the object
• Useful for one-time use or quick custom behaviour
GUI Example with Anonymous
Inner Class
JButton b = new JButton("Click Me");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button Clicked");
}
});
• Common in Swing/AWT for event handling.
• using an anonymous inner class (declared inline) avoids writing
a completely separate class just to handle an event like a
button click. It keeps the code shorter and more readable.
Comparison Table
Type Access to Outer Static? Use Case
Static
Nested
Static members
only
Yes Utility classes
Inner Class All members No
two classes are
strongly
dependent on
each other
Local Inner
Local method
scope
No Limited usage
Anonymous All members No One-time use
Best Practices
 Use static nested class for utility/helper classes.
 Use inner classes when tightly bound to outer class
logic.
 Local inner classes are good for encapsulating
temporary logic
 Anonymous classes are best suited for quick overrides.
Conclusion
• Nested classes help in logically structuring classes.
• Provide encapsulation and code grouping.
• Use the appropriate type based on your design
requirements.

More Related Content

PPT
A1771937735_21789_14_2018__16_ Nested Classes.ppt
PPT
Inner classes ,annoumous and outer classes in java
PPTX
Java Programming inner and Nested classes.pptx
PPTX
WINSEMFRE2024-25_CSE2005_ETH_AP2024255000715_2025-03-18_Reference-Material-I....
ODP
Synapseindia reviews.odp.
PPT
Java tutorials
PPT
JavaTutorials.ppt
PPTX
Static Import and access modifiers
A1771937735_21789_14_2018__16_ Nested Classes.ppt
Inner classes ,annoumous and outer classes in java
Java Programming inner and Nested classes.pptx
WINSEMFRE2024-25_CSE2005_ETH_AP2024255000715_2025-03-18_Reference-Material-I....
Synapseindia reviews.odp.
Java tutorials
JavaTutorials.ppt
Static Import and access modifiers

Similar to Java Nested classes, static class and methods, nested blocks_Inner_Classes.pptx (20)

PPT
Java static keyword
PPT
Java static keyword
PPTX
Nested class
PPTX
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
PPTX
Abstract Class & Abstract Method in Core Java
PPTX
Statics in java | Constructors | Exceptions in Java | String in java| class 3
PPTX
Modules 333333333³3444444444444444444.pptx
PDF
Java Inner Classes
PPTX
Nested classes in java
PPT
Java Tutorial
PPTX
class as the basis.pptx
PDF
OOPs & Inheritance Notes
DOCX
Nested class in java
PPTX
Class introduction in java
PPTX
Object Oriented Programming Inheritance with case study
PPTX
BCA Class and Object (3).pptx
PPT
Data Hiding and Data Encapsulation of java
PPTX
Classes and objects
Java static keyword
Java static keyword
Nested class
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
Abstract Class & Abstract Method in Core Java
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Modules 333333333³3444444444444444444.pptx
Java Inner Classes
Nested classes in java
Java Tutorial
class as the basis.pptx
OOPs & Inheritance Notes
Nested class in java
Class introduction in java
Object Oriented Programming Inheritance with case study
BCA Class and Object (3).pptx
Data Hiding and Data Encapsulation of java
Classes and objects
Ad

More from Satyanandaram Nandigam (10)

PDF
Introduction_SE_Modifiedsoftware engineering.pdf
PPTX
Data Modelling Conceptual level physical.pptx
PPTX
Basics_of_Object Oriented Programming_in_CPP.pptx
PPTX
Java_Access_Specifiers_and_Data_Types.pptx
PPT
software Design.ppt
PPT
C++_overloading.ppt
PPT
Swing_Introduction.ppt
PPTX
Review of C.pptx
PPTX
Introduction_SE_Modifiedsoftware engineering.pdf
Data Modelling Conceptual level physical.pptx
Basics_of_Object Oriented Programming_in_CPP.pptx
Java_Access_Specifiers_and_Data_Types.pptx
software Design.ppt
C++_overloading.ppt
Swing_Introduction.ppt
Review of C.pptx
Ad

Recently uploaded (20)

PPTX
Sustainable Sites - Green Building Construction
PPT
introduction to datamining and warehousing
PPTX
Geodesy 1.pptx...............................................
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Artificial Intelligence
PPTX
Safety Seminar civil to be ensured for safe working.
DOCX
573137875-Attendance-Management-System-original
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
Well-logging-methods_new................
PPT
Project quality management in manufacturing
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
PPT on Performance Review to get promotions
Sustainable Sites - Green Building Construction
introduction to datamining and warehousing
Geodesy 1.pptx...............................................
CYBER-CRIMES AND SECURITY A guide to understanding
R24 SURVEYING LAB MANUAL for civil enggi
Model Code of Practice - Construction Work - 21102022 .pdf
bas. eng. economics group 4 presentation 1.pptx
Artificial Intelligence
Safety Seminar civil to be ensured for safe working.
573137875-Attendance-Management-System-original
Internet of Things (IOT) - A guide to understanding
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Well-logging-methods_new................
Project quality management in manufacturing
Lecture Notes Electrical Wiring System Components
UNIT 4 Total Quality Management .pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPT on Performance Review to get promotions

Java Nested classes, static class and methods, nested blocks_Inner_Classes.pptx

  • 2. Introduction  Java allows classes to be defined within other classes.  These are called Nested Classes.  They help in logically grouping classes and controlling visibility.  Nested classes can access out class members and improve code modularity
  • 3. Types of Nested Classes 1. Static Nested Class 2. Non-static Nested Class (Inner Class) - Member Inner Class - Local Inner Class - Anonymous Inner Class
  • 4. Static Nested Class - Basic Example class Outer { static int a = 10; static class StaticNested { void show() { System.out.println("Static a: " + a); } } } Usage:  Outer.StaticNested obj = new Outer.StaticNested();  obj.show(); •Defined using static keyword inside another class •Can access only static members of the outer class.
  • 5. Static Nested Class - Advanced Example class MathUtil { static class Calculator { static int square(int x) { return x * x; } } }  Usage: int result = MathUtil.Calculator.square(5); System.out.println(result); // 25
  • 6. Static Nested Class - Benefits • Does not require access to outer class instance. • Useful for grouping static helpers. • Static nested classes allow you to organize related static methods and constants within a containing class. • This helps keep your code more structured and modular, especially for tools, calculators, or common operations. • Ideal for utility classes — classes that provide commonly used static methods.
  • 7. Inner Class - Basic Example class Outer { int a = 10; class Inner { void display() { System.out.println("a: " + a); } } }  Usage: Outer outer = new Outer(); Outer.Inner inner = outer.new Inner(); inner.display(); •Non-static class defined inside another class. •Can access both static and non-static members of the outer class. •Needs an object of the outer class to be created.
  • 8. Inner Class - Advanced Example class Bank { private String bankName = "ABC Bank"; class Account { void display() { System.out.println("Welcome to " + bankName); } } }  Usage: Bank bank = new Bank(); Bank.Account acc = bank.new Account(); acc.display(); Advantages: • Inner class has full access to outer class members. • Ideal when the inner class depends heavily on the outer class for its functionality.
  • 9. Local Inner Class class Outer { void outerMethod() { class LocalInner { void msg() { System.out.println("Inside Local Inner Class"); } } LocalInner obj = new LocalInner(); obj.msg(); } } Key Points: •Defined inside a method. •Can access final or effectively final variables from method.
  • 10. Anonymous Inner Class abstract class Animal { abstract void sound(); } class Test { public static void main(String args[]) { Animal dog = new Animal() { void sound() { System.out.println("Woof Woof"); } }; dog.sound(); } } When to use: • When creating an instance of class with certain methods overridden. • We need to redefining or implementing a method immediately while creating the object • Useful for one-time use or quick custom behaviour
  • 11. GUI Example with Anonymous Inner Class JButton b = new JButton("Click Me"); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Button Clicked"); } }); • Common in Swing/AWT for event handling. • using an anonymous inner class (declared inline) avoids writing a completely separate class just to handle an event like a button click. It keeps the code shorter and more readable.
  • 12. Comparison Table Type Access to Outer Static? Use Case Static Nested Static members only Yes Utility classes Inner Class All members No two classes are strongly dependent on each other Local Inner Local method scope No Limited usage Anonymous All members No One-time use
  • 13. Best Practices  Use static nested class for utility/helper classes.  Use inner classes when tightly bound to outer class logic.  Local inner classes are good for encapsulating temporary logic  Anonymous classes are best suited for quick overrides.
  • 14. Conclusion • Nested classes help in logically structuring classes. • Provide encapsulation and code grouping. • Use the appropriate type based on your design requirements.