SlideShare a Scribd company logo
9
Most read
10
Most read
15
Most read
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 (20)

PPTX
Java exception handling
BHUVIJAYAVELU
 
PPT
20. Object-Oriented Programming Fundamental Principles
Intro C# Book
 
PPT
9. Input Output in java
Nilesh Dalvi
 
PPT
L11 array list
teach4uin
 
ODP
Multithreading In Java
parag
 
PPTX
Classes and Objects in C#
Adeel Rasheed
 
PPTX
Inheritance In Java
Manish Sahu
 
PDF
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
PPTX
INHERITANCE IN JAVA.pptx
NITHISG1
 
PPTX
Ppt on this and super keyword
tanu_jaswal
 
PPTX
Classes objects in java
Madishetty Prathibha
 
PDF
Python programming : Abstract classes interfaces
Emertxe Information Technologies Pvt Ltd
 
PPT
Java inheritance
Arati Gadgil
 
PPTX
Java constructors
QUONTRASOLUTIONS
 
PPT
Inheritance in java
Lovely Professional University
 
PPTX
Inheritance in C++
Laxman Puri
 
PPTX
Constructor in java
Hitesh Kumar
 
PPTX
Classes,object and methods java
Padma Kannan
 
PPTX
Methods and constructors in java
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Polymorphism presentation in java
Ahsan Raja
 
Java exception handling
BHUVIJAYAVELU
 
20. Object-Oriented Programming Fundamental Principles
Intro C# Book
 
9. Input Output in java
Nilesh Dalvi
 
L11 array list
teach4uin
 
Multithreading In Java
parag
 
Classes and Objects in C#
Adeel Rasheed
 
Inheritance In Java
Manish Sahu
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
INHERITANCE IN JAVA.pptx
NITHISG1
 
Ppt on this and super keyword
tanu_jaswal
 
Classes objects in java
Madishetty Prathibha
 
Python programming : Abstract classes interfaces
Emertxe Information Technologies Pvt Ltd
 
Java inheritance
Arati Gadgil
 
Java constructors
QUONTRASOLUTIONS
 
Inheritance in java
Lovely Professional University
 
Inheritance in C++
Laxman Puri
 
Constructor in java
Hitesh Kumar
 
Classes,object and methods java
Padma Kannan
 
Methods and constructors in java
baabtra.com - No. 1 supplier of quality freshers
 
Polymorphism presentation in java
Ahsan Raja
 

Similar to Nested classes in java (20)

PPT
A1771937735_21789_14_2018__16_ Nested Classes.ppt
RithwikRanjan
 
PPTX
Java Nested class Concept
jagriti srivastava
 
PPTX
Javasession8
Rajeev Kumar
 
PPTX
types of classes in java
Nouman Riaz
 
PPTX
Inner class
Guna Sekaran
 
DOCX
Nested class in java
ChiradipBhattacharya
 
DOCX
Nested classes in java
Richa Singh
 
PDF
Inner Classes in Java
Dallington Asingwire
 
PPT
Lecture09.ppt
hemanth248901
 
PPTX
object oriented programming unit two ppt
isiagnel2
 
PDF
[圣思园][Java SE]Inner class
ArBing Xie
 
PDF
Inner class
Medivh2011
 
PPTX
Java Programming inner and Nested classes.pptx
AkashJha84
 
PPTX
Nested class
Daman Toor
 
PPTX
Inner class
Bansari Shah
 
PPT
Inner classes ,annoumous and outer classes in java
Adil Mehmoood
 
PPTX
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
 
PPT
Inner classes9 cm604.28
myrajendra
 
PPTX
Object oriented programming CLASSES-AND-OBJECTS.pptx
DaveEstonilo
 
PPTX
Java Inner Class
DeeptiJava
 
A1771937735_21789_14_2018__16_ Nested Classes.ppt
RithwikRanjan
 
Java Nested class Concept
jagriti srivastava
 
Javasession8
Rajeev Kumar
 
types of classes in java
Nouman Riaz
 
Inner class
Guna Sekaran
 
Nested class in java
ChiradipBhattacharya
 
Nested classes in java
Richa Singh
 
Inner Classes in Java
Dallington Asingwire
 
Lecture09.ppt
hemanth248901
 
object oriented programming unit two ppt
isiagnel2
 
[圣思园][Java SE]Inner class
ArBing Xie
 
Inner class
Medivh2011
 
Java Programming inner and Nested classes.pptx
AkashJha84
 
Nested class
Daman Toor
 
Inner class
Bansari Shah
 
Inner classes ,annoumous and outer classes in java
Adil Mehmoood
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
 
Inner classes9 cm604.28
myrajendra
 
Object oriented programming CLASSES-AND-OBJECTS.pptx
DaveEstonilo
 
Java Inner Class
DeeptiJava
 
Ad

Recently uploaded (20)

PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Ad

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.