SlideShare a Scribd company logo
Java/J2ee Programming Training
Inner Classes
Page 1Classification: Restricted
Agenda
• Inner Classes
Page 2Classification: Restricted
Objective
• Write code to construct instances of any concrete class including normal
top level classes, inner classes, static inner classes, and anonymous inner
classes.
Page 3Classification: Restricted
Inner Classes
• Inner classes can be non-static, static, method-local or anonymous
• An inner class instance has access to all the members of the outer class
including the private ones
Non-Static Inner Classes
• A regular or non-static inner class cannot have any static declarations
• The only way to access such a class is through an instance of the outer
class
Page 4Classification: Restricted
Example
Eg:
class MyClass {
private int i=10;
public void func() {
MyInner in=new MyInner();
in.callMe();
}
class MyInner {
public void callMe() {
System.out.println(“Value of i is “+i);
} }
}
Page 5Classification: Restricted
Instantiating an Inner Class
• To instantiate an inner class, you need an instance of the outer class
• From inside the outer class instance code, use the inner class name alone
to instantiate
Eg: MyInner myInner=new MyInner();
• From outside the outer class instance code, the inner class name must
include the outer class name
Eg: MyClass myClass=new MyClass();
MyClass.MyInner inner=myClass.new MyInner();
Page 6Classification: Restricted
Method local Inner Classes
• A method local inner class can be instantiated only within the method
where it is defined
• It cannot access the local variables of the enclosing method unless they
are final
Eg: class MyOuter {
private int i=2;
void callMe() {
class MyInner {
void callInner() {
System.out.println(“i is “+i);
}
}
} }
Page 7Classification: Restricted
Anonymous Inner Classes
• Anonymous inner classes have no name, and their type must be either a
subclass of the named type or an implementer of the named interface
Eg:
interface MusicalInstrument{ void play(); }
class Test{
MusicalInstrument m=new MusicalInstrument() {
public void play() {
System.out.println(“playing”);
} }; }
/* Instantiating an anonymous implementation class of the
MusicalInstrument interface */
Page 8Classification: Restricted
Static Nested Classes
• A static nested class is a static member of the enclosing class
• A static nested class does not have access to the instance variables and
methods of the class.
Eg:
class Outer{
static class MyNested{}
}
class Test{
public static void main(String args[]){
Outer.MyNested n=new Outer.MyNested();
}
}
Page 9Classification: Restricted
Extra Points to Remember…
• To refer to the inner class instance, use the keyword this from code
within the inner class. To reference the outer this, precede the keyword
this with the outer class name.
Eg: MyOuter.this
• The only modifiers that can be applied to a method local inner class are
abstract and final
• An anonymous subclass can extend a class or implement one interface,
not both.
• You don’t need an instance of the outer class to instantiate a static
nested class.
Page 10Classification: Restricted
Thank You

More Related Content

PPTX
Inner class
DOCX
Nested classes in java
PPT
Inner classes ,annoumous and outer classes in java
PPTX
Java- Nested Classes
PPTX
Inner classes
DOCX
Nested class in java
PPT
L5 classes, objects, nested and inner class
PDF
Java Inner Classes
Inner class
Nested classes in java
Inner classes ,annoumous and outer classes in java
Java- Nested Classes
Inner classes
Nested class in java
L5 classes, objects, nested and inner class
Java Inner Classes

What's hot (17)

PPTX
Inner classes in java
PPTX
Inner Classes & Multi Threading in JAVA
PDF
Inner Classes in Java
PPTX
C# Access modifiers
PPTX
C# Inheritance
PPT
Inner Classes
PPTX
[OOP - Lec 07] Access Specifiers
PDF
Classes and Nested Classes in Java
PPTX
Nested classes in java
PPTX
Access specifier
PPTX
Inner class
PPTX
types of classes in java
PDF
[圣思园][Java SE]Inner class
PPTX
C# classes objects
PPTX
Java Nested class Concept
PPT
java tutorial 4
Inner classes in java
Inner Classes & Multi Threading in JAVA
Inner Classes in Java
C# Access modifiers
C# Inheritance
Inner Classes
[OOP - Lec 07] Access Specifiers
Classes and Nested Classes in Java
Nested classes in java
Access specifier
Inner class
types of classes in java
[圣思园][Java SE]Inner class
C# classes objects
Java Nested class Concept
java tutorial 4
Ad

Similar to Java Inner Class (19)

PPTX
Session 21 - Inner Classes
PPSX
Inner Classes
PPT
A1771937735_21789_14_2018__16_ Nested Classes.ppt
PPTX
Java Programming inner and Nested classes.pptx
PPTX
Nested class
PPTX
Java Nested classes, static class and methods, nested blocks_Inner_Classes.pptx
PPTX
WINSEMFRE2024-25_CSE2005_ETH_AP2024255000715_2025-03-18_Reference-Material-I....
PDF
Java Inner Classes
PPTX
Static Members-Java.pptx
PDF
Inner class
PDF
Developing Applications for Android - Lecture#2
PPTX
Javasession8
PPT
Inner classes9 cm604.28
PPTX
Static Import and access modifiers
PPTX
Classes and objects
PPTX
[Java] #8 String and Inner Class
PPTX
03 classes interfaces_principlesofoop
PPTX
OBJECT ORIENTED PROGRAMMING_Unit2_firsthalf Updated.pptx
ODP
Class&objects
Session 21 - Inner Classes
Inner Classes
A1771937735_21789_14_2018__16_ Nested Classes.ppt
Java Programming inner and Nested classes.pptx
Nested class
Java Nested classes, static class and methods, nested blocks_Inner_Classes.pptx
WINSEMFRE2024-25_CSE2005_ETH_AP2024255000715_2025-03-18_Reference-Material-I....
Java Inner Classes
Static Members-Java.pptx
Inner class
Developing Applications for Android - Lecture#2
Javasession8
Inner classes9 cm604.28
Static Import and access modifiers
Classes and objects
[Java] #8 String and Inner Class
03 classes interfaces_principlesofoop
OBJECT ORIENTED PROGRAMMING_Unit2_firsthalf Updated.pptx
Class&objects
Ad

More from DeeptiJava (13)

PPT
Generating the Server Response: HTTP Status Codes
PPTX
Java Generics
PPTX
Java Collection
PPTX
Java Exception Handling
PPTX
Java OOPs
PPTX
Java Access Specifier
PPTX
Java JDBC
PPTX
Java Thread
PPT
JSP Part 2
PPT
JSP Part 1
PPTX
Java I/O
PPT
Java Hibernate Basics
PPTX
Introduction to Java
Generating the Server Response: HTTP Status Codes
Java Generics
Java Collection
Java Exception Handling
Java OOPs
Java Access Specifier
Java JDBC
Java Thread
JSP Part 2
JSP Part 1
Java I/O
Java Hibernate Basics
Introduction to Java

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Approach and Philosophy of On baking technology
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Spectroscopy.pptx food analysis technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Electronic commerce courselecture one. Pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Encapsulation theory and applications.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
20250228 LYD VKU AI Blended-Learning.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Approach and Philosophy of On baking technology
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Review of recent advances in non-invasive hemoglobin estimation
Spectroscopy.pptx food analysis technology
Reach Out and Touch Someone: Haptics and Empathic Computing
Machine learning based COVID-19 study performance prediction
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Electronic commerce courselecture one. Pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Encapsulation theory and applications.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Per capita expenditure prediction using model stacking based on satellite ima...
Network Security Unit 5.pdf for BCA BBA.
Dropbox Q2 2025 Financial Results & Investor Presentation
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Building Integrated photovoltaic BIPV_UPV.pdf

Java Inner Class

  • 3. Page 2Classification: Restricted Objective • Write code to construct instances of any concrete class including normal top level classes, inner classes, static inner classes, and anonymous inner classes.
  • 4. Page 3Classification: Restricted Inner Classes • Inner classes can be non-static, static, method-local or anonymous • An inner class instance has access to all the members of the outer class including the private ones Non-Static Inner Classes • A regular or non-static inner class cannot have any static declarations • The only way to access such a class is through an instance of the outer class
  • 5. Page 4Classification: Restricted Example Eg: class MyClass { private int i=10; public void func() { MyInner in=new MyInner(); in.callMe(); } class MyInner { public void callMe() { System.out.println(“Value of i is “+i); } } }
  • 6. Page 5Classification: Restricted Instantiating an Inner Class • To instantiate an inner class, you need an instance of the outer class • From inside the outer class instance code, use the inner class name alone to instantiate Eg: MyInner myInner=new MyInner(); • From outside the outer class instance code, the inner class name must include the outer class name Eg: MyClass myClass=new MyClass(); MyClass.MyInner inner=myClass.new MyInner();
  • 7. Page 6Classification: Restricted Method local Inner Classes • A method local inner class can be instantiated only within the method where it is defined • It cannot access the local variables of the enclosing method unless they are final Eg: class MyOuter { private int i=2; void callMe() { class MyInner { void callInner() { System.out.println(“i is “+i); } } } }
  • 8. Page 7Classification: Restricted Anonymous Inner Classes • Anonymous inner classes have no name, and their type must be either a subclass of the named type or an implementer of the named interface Eg: interface MusicalInstrument{ void play(); } class Test{ MusicalInstrument m=new MusicalInstrument() { public void play() { System.out.println(“playing”); } }; } /* Instantiating an anonymous implementation class of the MusicalInstrument interface */
  • 9. Page 8Classification: Restricted Static Nested Classes • A static nested class is a static member of the enclosing class • A static nested class does not have access to the instance variables and methods of the class. Eg: class Outer{ static class MyNested{} } class Test{ public static void main(String args[]){ Outer.MyNested n=new Outer.MyNested(); } }
  • 10. Page 9Classification: Restricted Extra Points to Remember… • To refer to the inner class instance, use the keyword this from code within the inner class. To reference the outer this, precede the keyword this with the outer class name. Eg: MyOuter.this • The only modifiers that can be applied to a method local inner class are abstract and final • An anonymous subclass can extend a class or implement one interface, not both. • You don’t need an instance of the outer class to instantiate a static nested class.