SlideShare a Scribd company logo
3
Most read
18
Most read
19
Most read
INNER CLASSES IN JAVA
https://dev.to/dallington256/inner-classes-or-nested-classes-
in-java-4m8d
Objectives
▪What are inner classes
▪Why do we need inner classes
▪Advantages of using inner classes
▪Types of inner classes
▪Code examples of each type of inner classes
Definition
An inner class is a class that is declared inside a class or
interface.
Inner classes are used to logically group classes or interfaces in
one place in order to improve readability and maintainability.
Creating an inner class is quite simple. You just need to write a
class within a class. Unlike a class, an inner class can be private
and once you declare an inner class private, it cannot be accessed
from an object outside the class
Syntax of an inner class
class x1 {
// code
class x2{
// code
}
}
Why do we need an inner class
There might be a program where a class must not be accessed
by the other class so in that case, you include it in another
class.
Inner classes are a security mechanism in Java. We know a
class cannot be associated with the access modifier private,
but if we have the class as a member of other class, then the
inner class can be made private. And this is also used to
access the private members of a class
Advantages of using inner classes
▪Because inner classes logically group classes and interfaces
in one place only, they improve readability and
maintainability.
▪Inner classes are a representation of a particular type of
relationship in that inner class can access all the members
(data members and methods) of an outer class including one
declared private.
▪Minimizes coding efforts since there’s less code to write
thus facilitating code optimization.
Key facts on the relationship between
inner and outer classes
▪Inner and outer classes have access to each other’s private
members.
Within the definition of a method of an inner classes;
❖ It is legal to reference a private instance variable of the
outer class
❖ It is legal to invoke a private method of the outer class
Key facts on the relationship between
inner and outer classes
Within the definition of a method of the outer classes;
❖ It is legal to reference a private instance variable of the
inner class on an object of the inner class.
❖ It is legal to invoke a (non-static) method of the inner class
as long as an object of the inner class is used as a calling
object.
❖ Within the definition of the inner or outer class, the
modifiers public and private are equivalent.
Types of inner classes
There are 4 types of inner classes i.e..
1. Member class (Nested inner class)
2. Static member inner class (Static nested class)
3. Anonymous inner class
4. Local inner class.
1. Member inner class
A member inner class is a class created within a class and
outside a method. A member inner class is non-static.
A member inner class refers directly to its instance variables or
methods defined in its enclosing class. Also because an inner
class is associated with an instance, it cannot define any static
members itself.
Syntax for member inner class
class MainClass{
// code
class InnerClass{
// code
}
Instantiation of a member inner class
Syntax for creating an object of a non-static member
inner class
MainClass m = new MainClass();
MainClass.InnerClass innerObj = m.new InnerClass()
Example
Write simple example to demonstrate member inner class
Dev Link: https://dev.to/dallington256/inner-classes-or-nested-
classes-in-java-4m8d
GitHub Link :https://github.com/DallingtonAsin/BSSE3209.git
2. Static member inner class
A static member inner class is an inner class that is declared static.
Syntax for a static inner class
class MainClass {
// code
static class StaticInnerClass {
// code
}
}
Continue…
In the above code snippet, StaticInnerClass is a static inner class.
This class can’t refer directly to their instance variables or methods
defined in its enclosing class since they use them only by an object
reference.
They are accessed by the use of class name.
Syntax for creating an object of a static inner class
MainClass .StaticInnerClass objectName = new
MainClass.StaticInnerClass();
Example
Write simple example to demonstrate a static member inner class
Dev Link: https://dev.to/dallington256/inner-classes-or-nested-
classes-in-java-4m8d
GitHub Link :https://github.com/DallingtonAsin/BSSE3209.git
Facts
●Compiling any class in java produces a .class file named
ClassName.class
●Compiling a class with one (or more) inner classes causes both(or
more) classes to be compiled, and produce two(or more) . class files.
E.g ClassName.class and ClassName$innerClassName.class
3. Anonymous inner class
Anonymous inner class is one that is created for implementing an interface or
extending a class. Thereafter, anonymous class must implement an interface or
extend an abstract class but you don’t use keywords extend or implement to
create anonymous class.
Syntax for anonymous inner class
AnonymousInner an_inner = new AnonymousInner() {
public void my_method() {
// code
}
};
Key facts about anonymous inner class
▪Anonymous inner class cannot have a constructor. Since they have
no name, we can't use them in order to create instances of
anonymous classes. As a result, we have to declare and instantiate
anonymous classes in a single expression at the point of use. We
may either extend an existing class or implement an interface.
▪Anonymous class can access any variables visible to the block
within which anonymous class is declared, including local
variables.
▪Anonymous class can also access methods of a class that contains
it.
Example
Write simple example to demonstrate anonymous inner
class
Dev Link: https://dev.to/dallington256/inner-classes-or-nested-
classes-in-java-4m8d
GitHub Link :https://github.com/DallingtonAsin/BSSE3209.git
4. Local inner classes
A local inner class is one that is defined in a block, which is a group of
zero or more statements between balanced braces. You typically find
local classes defined in the body of a method.
Example
public class Outerclass {
// instance method of the outer class
void my_Method() {
int num = 23;
// method-local inner class
class MethodInner_Demo {
public void print() {
System.out.println("This is method inner class "+num);
}
} // end of inner class
// Accessing the inner class
MethodInner_Demo inner = new MethodInner_Demo();
inner.print();
}
public static void main(String args[]) {
Outerclass outer = new Outerclass();
outer.my_Method();
}
}
Assignment
Use real life examples of your choice to illustrate the use of
following concepts in java programming;
●Member inner classes;
●Static inner classes
●Anonymous inner classes
●Local inner classes
●Read about helper classes in Java

More Related Content

What's hot (20)

PPTX
Virtual function and abstract class
Shweta Shah
 
PPTX
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
PPTX
Pure virtual function and abstract class
Amit Trivedi
 
PPT
9. Input Output in java
Nilesh Dalvi
 
PDF
C++ Files and Streams
Ahmed Farag
 
PPSX
Collections - Lists, Sets
Hitesh-Java
 
PPTX
Interfaces in java
Abishek Purushothaman
 
PPT
Java multi threading
Raja Sekhar
 
PPTX
This keyword in java
Hitesh Kumar
 
PPTX
C# classes objects
Dr.Neeraj Kumar Pandey
 
PPT
Swing and AWT in java
Adil Mehmoood
 
PPTX
Inner class
Guna Sekaran
 
PPSX
Exception Handling
Reddhi Basu
 
PPT
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
PPTX
Object Oriented Programming Using C++
Muhammad Waqas
 
PPTX
Java exception handling
BHUVIJAYAVELU
 
PPT
Inner classes ,annoumous and outer classes in java
Adil Mehmoood
 
PPT
Inheritance in java
Lovely Professional University
 
PPTX
Java class,object,method introduction
Sohanur63
 
Virtual function and abstract class
Shweta Shah
 
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
Pure virtual function and abstract class
Amit Trivedi
 
9. Input Output in java
Nilesh Dalvi
 
C++ Files and Streams
Ahmed Farag
 
Collections - Lists, Sets
Hitesh-Java
 
Interfaces in java
Abishek Purushothaman
 
Java multi threading
Raja Sekhar
 
This keyword in java
Hitesh Kumar
 
C# classes objects
Dr.Neeraj Kumar Pandey
 
Swing and AWT in java
Adil Mehmoood
 
Inner class
Guna Sekaran
 
Exception Handling
Reddhi Basu
 
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
Object Oriented Programming Using C++
Muhammad Waqas
 
Java exception handling
BHUVIJAYAVELU
 
Inner classes ,annoumous and outer classes in java
Adil Mehmoood
 
Inheritance in java
Lovely Professional University
 
Java class,object,method introduction
Sohanur63
 

Similar to Inner Classes in Java (20)

PPT
A1771937735_21789_14_2018__16_ Nested Classes.ppt
RithwikRanjan
 
PPTX
Inner Classes & Multi Threading in JAVA
Tech_MX
 
DOCX
Nested classes in java
Richa Singh
 
PPTX
Java Programming inner and Nested classes.pptx
AkashJha84
 
DOCX
Nested class in java
ChiradipBhattacharya
 
PPTX
Inner class
Bansari Shah
 
PPTX
Javasession8
Rajeev Kumar
 
PPTX
Nested classes in java
ChiradipBhattacharya
 
PPTX
WINSEMFRE2024-25_CSE2005_ETH_AP2024255000715_2025-03-18_Reference-Material-I....
belgiumsckgr
 
PPTX
Nested class
Daman Toor
 
PPTX
Java Nested class Concept
jagriti srivastava
 
PPTX
Inner classes
DraftKing Zohaib
 
PPTX
types of classes in java
Nouman Riaz
 
PPTX
Java- Nested Classes
Prabhdeep Singh
 
PPTX
Object oriented programming CLASSES-AND-OBJECTS.pptx
DaveEstonilo
 
PDF
Classes in Java great learning.pdf
SHASHIKANT346021
 
PDF
Java Inner Classes
Jussi Pohjolainen
 
PDF
Java Inner Classes
Soba Arjun
 
PPT
Inner Classes
parag
 
PPTX
Java Inner Class
DeeptiJava
 
A1771937735_21789_14_2018__16_ Nested Classes.ppt
RithwikRanjan
 
Inner Classes & Multi Threading in JAVA
Tech_MX
 
Nested classes in java
Richa Singh
 
Java Programming inner and Nested classes.pptx
AkashJha84
 
Nested class in java
ChiradipBhattacharya
 
Inner class
Bansari Shah
 
Javasession8
Rajeev Kumar
 
Nested classes in java
ChiradipBhattacharya
 
WINSEMFRE2024-25_CSE2005_ETH_AP2024255000715_2025-03-18_Reference-Material-I....
belgiumsckgr
 
Nested class
Daman Toor
 
Java Nested class Concept
jagriti srivastava
 
Inner classes
DraftKing Zohaib
 
types of classes in java
Nouman Riaz
 
Java- Nested Classes
Prabhdeep Singh
 
Object oriented programming CLASSES-AND-OBJECTS.pptx
DaveEstonilo
 
Classes in Java great learning.pdf
SHASHIKANT346021
 
Java Inner Classes
Jussi Pohjolainen
 
Java Inner Classes
Soba Arjun
 
Inner Classes
parag
 
Java Inner Class
DeeptiJava
 
Ad

Recently uploaded (20)

PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
Ad

Inner Classes in Java

  • 1. INNER CLASSES IN JAVA https://dev.to/dallington256/inner-classes-or-nested-classes- in-java-4m8d
  • 2. Objectives ▪What are inner classes ▪Why do we need inner classes ▪Advantages of using inner classes ▪Types of inner classes ▪Code examples of each type of inner classes
  • 3. Definition An inner class is a class that is declared inside a class or interface. Inner classes are used to logically group classes or interfaces in one place in order to improve readability and maintainability. Creating an inner class is quite simple. You just need to write a class within a class. Unlike a class, an inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class
  • 4. Syntax of an inner class class x1 { // code class x2{ // code } }
  • 5. Why do we need an inner class There might be a program where a class must not be accessed by the other class so in that case, you include it in another class. Inner classes are a security mechanism in Java. We know a class cannot be associated with the access modifier private, but if we have the class as a member of other class, then the inner class can be made private. And this is also used to access the private members of a class
  • 6. Advantages of using inner classes ▪Because inner classes logically group classes and interfaces in one place only, they improve readability and maintainability. ▪Inner classes are a representation of a particular type of relationship in that inner class can access all the members (data members and methods) of an outer class including one declared private. ▪Minimizes coding efforts since there’s less code to write thus facilitating code optimization.
  • 7. Key facts on the relationship between inner and outer classes ▪Inner and outer classes have access to each other’s private members. Within the definition of a method of an inner classes; ❖ It is legal to reference a private instance variable of the outer class ❖ It is legal to invoke a private method of the outer class
  • 8. Key facts on the relationship between inner and outer classes Within the definition of a method of the outer classes; ❖ It is legal to reference a private instance variable of the inner class on an object of the inner class. ❖ It is legal to invoke a (non-static) method of the inner class as long as an object of the inner class is used as a calling object. ❖ Within the definition of the inner or outer class, the modifiers public and private are equivalent.
  • 9. Types of inner classes There are 4 types of inner classes i.e.. 1. Member class (Nested inner class) 2. Static member inner class (Static nested class) 3. Anonymous inner class 4. Local inner class.
  • 10. 1. Member inner class A member inner class is a class created within a class and outside a method. A member inner class is non-static. A member inner class refers directly to its instance variables or methods defined in its enclosing class. Also because an inner class is associated with an instance, it cannot define any static members itself.
  • 11. Syntax for member inner class class MainClass{ // code class InnerClass{ // code }
  • 12. Instantiation of a member inner class Syntax for creating an object of a non-static member inner class MainClass m = new MainClass(); MainClass.InnerClass innerObj = m.new InnerClass()
  • 13. Example Write simple example to demonstrate member inner class Dev Link: https://dev.to/dallington256/inner-classes-or-nested- classes-in-java-4m8d GitHub Link :https://github.com/DallingtonAsin/BSSE3209.git
  • 14. 2. Static member inner class A static member inner class is an inner class that is declared static. Syntax for a static inner class class MainClass { // code static class StaticInnerClass { // code } }
  • 15. Continue… In the above code snippet, StaticInnerClass is a static inner class. This class can’t refer directly to their instance variables or methods defined in its enclosing class since they use them only by an object reference. They are accessed by the use of class name. Syntax for creating an object of a static inner class MainClass .StaticInnerClass objectName = new MainClass.StaticInnerClass();
  • 16. Example Write simple example to demonstrate a static member inner class Dev Link: https://dev.to/dallington256/inner-classes-or-nested- classes-in-java-4m8d GitHub Link :https://github.com/DallingtonAsin/BSSE3209.git
  • 17. Facts ●Compiling any class in java produces a .class file named ClassName.class ●Compiling a class with one (or more) inner classes causes both(or more) classes to be compiled, and produce two(or more) . class files. E.g ClassName.class and ClassName$innerClassName.class
  • 18. 3. Anonymous inner class Anonymous inner class is one that is created for implementing an interface or extending a class. Thereafter, anonymous class must implement an interface or extend an abstract class but you don’t use keywords extend or implement to create anonymous class. Syntax for anonymous inner class AnonymousInner an_inner = new AnonymousInner() { public void my_method() { // code } };
  • 19. Key facts about anonymous inner class ▪Anonymous inner class cannot have a constructor. Since they have no name, we can't use them in order to create instances of anonymous classes. As a result, we have to declare and instantiate anonymous classes in a single expression at the point of use. We may either extend an existing class or implement an interface. ▪Anonymous class can access any variables visible to the block within which anonymous class is declared, including local variables. ▪Anonymous class can also access methods of a class that contains it.
  • 20. Example Write simple example to demonstrate anonymous inner class Dev Link: https://dev.to/dallington256/inner-classes-or-nested- classes-in-java-4m8d GitHub Link :https://github.com/DallingtonAsin/BSSE3209.git
  • 21. 4. Local inner classes A local inner class is one that is defined in a block, which is a group of zero or more statements between balanced braces. You typically find local classes defined in the body of a method.
  • 22. Example public class Outerclass { // instance method of the outer class void my_Method() { int num = 23; // method-local inner class class MethodInner_Demo { public void print() { System.out.println("This is method inner class "+num); } } // end of inner class // Accessing the inner class MethodInner_Demo inner = new MethodInner_Demo(); inner.print(); } public static void main(String args[]) { Outerclass outer = new Outerclass(); outer.my_Method(); } }
  • 23. Assignment Use real life examples of your choice to illustrate the use of following concepts in java programming; ●Member inner classes; ●Static inner classes ●Anonymous inner classes ●Local inner classes ●Read about helper classes in Java