SlideShare a Scribd company logo
1 of 18
NESTED CLASSES IN JAVA
By Chiradip Bhattacharya
CSE, A,13000216109
Kinds of Classes in Java
• Top level or Outer classes
 Declared inside package
 Visible throughout package, perhaps further
 Normally, although not always, declared in their own file
• public classes must be defined in their own file
• Nested and inner classes
 Declared inside class (or method)
 Can be visible only to outer class, or have wider visibility
Nested Classes
Writing a class within another class is allowed in
Java. The class written within is called the nested
class, and the class that holds the inner class is
called the outer class.
Syntax :
Here Outer_Demo is the outer class and Nested_Demo is the
nested class.
Types of Nested Classes
• Non-static nested classes :
These are the non-static classes also known as Inner
classes, which are not declared with the static keyword.
They can be further divided into –
 Instance Inner classes
 Method Local Inner classes
 Anonymous Inner classes
• Static nested classes :
These are the static classes declared with the static
classes.
Types of Nested Classes
Non-static Nested (Inner) Class
• It is the non-static nested class defined in the scope of
another class or interface.
• Can directly access all variables and methods of
enclosing class (including private fields and methods)
and is associated with an instance of its enclosing
class.
• They are of three types and can be divided into –
Instance Inner classes
Method Local Inner classes
Anonymous Inner classes
Instance Inner Class
• It is the most basic type of non-static nested class.
• It is created when a class is written inside another
class and the inner class can be used to access the
private members of the outer class.
• To instantiate the inner class, the outer class has to
be instantiated first. Then, using the object of the
outer class, the inner class can be instantiated and its
methods can be called.
Instance Inner Class
Syntax :
Method Local Inner Class
• It is the type of non-static nested class that is defined
in a block, typically in a method.
• Local inner class cannot be invoked from outside the
method.
• It can only access only the ‘final’ parameters of the
enclosing block, as it captures that variable or
parameter.
• Cannot have static data members (unless they are
declared final) and static methods.
Method Local Inner Class
Syntax : 1)
2)
Anonymous Inner Class
• Local classes with no name are called Anonymous classes.
• It helps to make code more concise by allowing to declare
and instantiate a class at the same time.
• It is used when a local class is to be used only once.
• It is often used to override method of a class or interface.
// Anonymous inner class for event handling
Accessing the private members of
an Inner Class
• To instantiate the inner class, the outer class has to be
instantiated first. Then, using the object of the outer class,
the inner class can be instantiated and its private members
can be accessed.
Accessing the private members of
an Inner Class (continued …)
• On compiling and executing the code, the result is –
• The ‘getNum()’ method of the inner class is called
from the other class after instantiating it with the
outer class.
• Syntax :
Static Nested Classes
• A static nested class is a nested class which is a
static member of the outer class. It can be
accessed without instantiating the outer class,
using other static members.
• Just like static members, a static nested class
does not have access to the instance variables
and methods of the outer class.
• Syntax :
Instantiating Static Nested Classes
• A static nested class is instantiated in a bit
different way from non – static inner classes.
• Syntax :
Instantiating Static Nested Classes
(continued …)
• On compiling and executing the code, the result
is –
• The ‘my_method()’ method of the nested class is
called from the other class after instantiating it
without instantiating the outer class.
• Syntax :
Advantages of Nested Classes
• It is a way of logically grouping classes that are only used in one place
• Can access all the members of the outer class including private data
members and methods.
• Can be declared private, public, protected or package private, unlike
regular classes that can be only declared public or package private.
• It increases encapsulation as itself can be declared private and still
access the outer class’ private members
• It can lead to more readable and maintainable code as it places the
code closer to where it is used.
THANK YOU

More Related Content

What's hot

Type casting in java
Type casting in javaType casting in java
Type casting in javaFarooq Baloch
 
Encapsulation in C++
Encapsulation in C++Encapsulation in C++
Encapsulation in C++Hitesh Kumar
 
Constants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaConstants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaAbhilash Nair
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++Sujan Mia
 
Java Data Types and Variables
Java Data Types and VariablesJava Data Types and Variables
Java Data Types and Variablessasi saseenthiran
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oopsHirra Sultan
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...Simplilearn
 
Core java concepts
Core java  conceptsCore java  concepts
Core java conceptsRam132
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming languageMd.Al-imran Roton
 

What's hot (20)

Type casting in java
Type casting in javaType casting in java
Type casting in java
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Encapsulation in C++
Encapsulation in C++Encapsulation in C++
Encapsulation in C++
 
Java packages
Java packagesJava packages
Java packages
 
Java program structure
Java program structureJava program structure
Java program structure
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Constants, Variables and Data Types in Java
Constants, Variables and Data Types in JavaConstants, Variables and Data Types in Java
Constants, Variables and Data Types in Java
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Java Data Types and Variables
Java Data Types and VariablesJava Data Types and Variables
Java Data Types and Variables
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
 
Java thread life cycle
Java thread life cycleJava thread life cycle
Java thread life cycle
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Java Basic Oops Concept
Java Basic Oops ConceptJava Basic Oops Concept
Java Basic Oops Concept
 

Similar to Nested classes in java

A1771937735_21789_14_2018__16_ Nested Classes.ppt
A1771937735_21789_14_2018__16_ Nested Classes.pptA1771937735_21789_14_2018__16_ Nested Classes.ppt
A1771937735_21789_14_2018__16_ Nested Classes.pptRithwikRanjan
 
types of classes in java
types of classes in javatypes of classes in java
types of classes in javaNouman Riaz
 
Nested classes in java
Nested classes in javaNested classes in java
Nested classes in javaRicha Singh
 
[圣思园][Java SE]Inner class
[圣思园][Java SE]Inner class[圣思园][Java SE]Inner class
[圣思园][Java SE]Inner classArBing Xie
 
Inner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaInner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaAdil Mehmoood
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardHari kiran G
 
Inner classes9 cm604.28
Inner classes9 cm604.28Inner classes9 cm604.28
Inner classes9 cm604.28myrajendra
 
Object oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptxObject oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptxDaveEstonilo
 
Java Inner Class
Java Inner ClassJava Inner Class
Java Inner ClassDeeptiJava
 
Classes in Java great learning.pdf
Classes in Java great learning.pdfClasses in Java great learning.pdf
Classes in Java great learning.pdfSHASHIKANT346021
 
Chapter 03 enscapsulation
Chapter 03 enscapsulationChapter 03 enscapsulation
Chapter 03 enscapsulationNurhanna Aziz
 
Abstraction in java.pptx
Abstraction in java.pptxAbstraction in java.pptx
Abstraction in java.pptxAsifMulani17
 

Similar to Nested classes in java (20)

A1771937735_21789_14_2018__16_ Nested Classes.ppt
A1771937735_21789_14_2018__16_ Nested Classes.pptA1771937735_21789_14_2018__16_ Nested Classes.ppt
A1771937735_21789_14_2018__16_ Nested Classes.ppt
 
Java Nested class Concept
Java Nested class ConceptJava Nested class Concept
Java Nested class Concept
 
Javasession8
Javasession8Javasession8
Javasession8
 
types of classes in java
types of classes in javatypes of classes in java
types of classes in java
 
Nested classes in java
Nested classes in javaNested classes in java
Nested classes in java
 
Inner Classes in Java
Inner Classes in JavaInner Classes in Java
Inner Classes in Java
 
Lecture09.ppt
Lecture09.pptLecture09.ppt
Lecture09.ppt
 
[圣思园][Java SE]Inner class
[圣思园][Java SE]Inner class[圣思园][Java SE]Inner class
[圣思园][Java SE]Inner class
 
Inner class
Inner classInner class
Inner class
 
Nested class
Nested classNested class
Nested class
 
Inner class
Inner classInner class
Inner class
 
Inner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaInner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in java
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
 
Inner classes9 cm604.28
Inner classes9 cm604.28Inner classes9 cm604.28
Inner classes9 cm604.28
 
Object oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptxObject oriented programming CLASSES-AND-OBJECTS.pptx
Object oriented programming CLASSES-AND-OBJECTS.pptx
 
Java Inner Class
Java Inner ClassJava Inner Class
Java Inner Class
 
Classes in Java great learning.pdf
Classes in Java great learning.pdfClasses in Java great learning.pdf
Classes in Java great learning.pdf
 
Inner classes
Inner classesInner classes
Inner classes
 
Chapter 03 enscapsulation
Chapter 03 enscapsulationChapter 03 enscapsulation
Chapter 03 enscapsulation
 
Abstraction in java.pptx
Abstraction in java.pptxAbstraction in java.pptx
Abstraction in java.pptx
 

Recently uploaded

Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 

Recently uploaded (20)

Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 

Nested classes in java

  • 1. NESTED CLASSES IN JAVA By Chiradip Bhattacharya CSE, A,13000216109
  • 2. Kinds of Classes in Java • Top level or Outer classes  Declared inside package  Visible throughout package, perhaps further  Normally, although not always, declared in their own file • public classes must be defined in their own file • Nested and inner classes  Declared inside class (or method)  Can be visible only to outer class, or have wider visibility
  • 3. Nested Classes Writing a class within another class is allowed in Java. The class written within is called the nested class, and the class that holds the inner class is called the outer class. Syntax : Here Outer_Demo is the outer class and Nested_Demo is the nested class.
  • 4. Types of Nested Classes • Non-static nested classes : These are the non-static classes also known as Inner classes, which are not declared with the static keyword. They can be further divided into –  Instance Inner classes  Method Local Inner classes  Anonymous Inner classes • Static nested classes : These are the static classes declared with the static classes.
  • 5. Types of Nested Classes
  • 6. Non-static Nested (Inner) Class • It is the non-static nested class defined in the scope of another class or interface. • Can directly access all variables and methods of enclosing class (including private fields and methods) and is associated with an instance of its enclosing class. • They are of three types and can be divided into – Instance Inner classes Method Local Inner classes Anonymous Inner classes
  • 7. Instance Inner Class • It is the most basic type of non-static nested class. • It is created when a class is written inside another class and the inner class can be used to access the private members of the outer class. • To instantiate the inner class, the outer class has to be instantiated first. Then, using the object of the outer class, the inner class can be instantiated and its methods can be called.
  • 9. Method Local Inner Class • It is the type of non-static nested class that is defined in a block, typically in a method. • Local inner class cannot be invoked from outside the method. • It can only access only the ‘final’ parameters of the enclosing block, as it captures that variable or parameter. • Cannot have static data members (unless they are declared final) and static methods.
  • 10. Method Local Inner Class Syntax : 1) 2)
  • 11. Anonymous Inner Class • Local classes with no name are called Anonymous classes. • It helps to make code more concise by allowing to declare and instantiate a class at the same time. • It is used when a local class is to be used only once. • It is often used to override method of a class or interface. // Anonymous inner class for event handling
  • 12. Accessing the private members of an Inner Class • To instantiate the inner class, the outer class has to be instantiated first. Then, using the object of the outer class, the inner class can be instantiated and its private members can be accessed.
  • 13. Accessing the private members of an Inner Class (continued …) • On compiling and executing the code, the result is – • The ‘getNum()’ method of the inner class is called from the other class after instantiating it with the outer class. • Syntax :
  • 14. Static Nested Classes • A static nested class is a nested class which is a static member of the outer class. It can be accessed without instantiating the outer class, using other static members. • Just like static members, a static nested class does not have access to the instance variables and methods of the outer class. • Syntax :
  • 15. Instantiating Static Nested Classes • A static nested class is instantiated in a bit different way from non – static inner classes. • Syntax :
  • 16. Instantiating Static Nested Classes (continued …) • On compiling and executing the code, the result is – • The ‘my_method()’ method of the nested class is called from the other class after instantiating it without instantiating the outer class. • Syntax :
  • 17. Advantages of Nested Classes • It is a way of logically grouping classes that are only used in one place • Can access all the members of the outer class including private data members and methods. • Can be declared private, public, protected or package private, unlike regular classes that can be only declared public or package private. • It increases encapsulation as itself can be declared private and still access the outer class’ private members • It can lead to more readable and maintainable code as it places the code closer to where it is used.