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

What's hot

Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
PhD Research Scholar
 
Inner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVAInner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVA
Tech_MX
 
Inner Classes in Java
Inner Classes in JavaInner Classes in Java
Inner Classes in Java
Dallington Asingwire
 
C# Access modifiers
C# Access modifiersC# Access modifiers
C# Access modifiers
Prem Kumar Badri
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
Prem Kumar Badri
 
Inner Classes
Inner ClassesInner Classes
Inner Classes
parag
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
Muhammad Hammad Waseem
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
Ravi_Kant_Sahu
 
Nested classes in java
Nested classes in javaNested classes in java
Nested classes in java
ChiradipBhattacharya
 
Access specifier
Access specifierAccess specifier
Access specifier
zindadili
 
Inner class
Inner classInner class
Inner class
Bansari Shah
 
types of classes in java
types of classes in javatypes of classes in java
types of classes in java
Nouman Riaz
 
[圣思园][Java SE]Inner class
[圣思园][Java SE]Inner class[圣思园][Java SE]Inner class
[圣思园][Java SE]Inner class
ArBing Xie
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
Java Nested class Concept
Java Nested class ConceptJava Nested class Concept
Java Nested class Concept
jagriti srivastava
 
Classes and objects
Classes and objectsClasses and objects
java tutorial 4
 java tutorial 4 java tutorial 4
java tutorial 4
Tushar Desarda
 

What's hot (17)

Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
Inner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVAInner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVA
 
Inner Classes in Java
Inner Classes in JavaInner Classes in Java
Inner Classes in Java
 
C# Access modifiers
C# Access modifiersC# Access modifiers
C# Access modifiers
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
 
Inner Classes
Inner ClassesInner Classes
Inner Classes
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 
Nested classes in java
Nested classes in javaNested classes in java
Nested classes in java
 
Access specifier
Access specifierAccess specifier
Access specifier
 
Inner class
Inner classInner class
Inner class
 
types of classes in java
types of classes in javatypes of classes in java
types of classes in java
 
[圣思园][Java SE]Inner class
[圣思园][Java SE]Inner class[圣思园][Java SE]Inner class
[圣思园][Java SE]Inner class
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Java Nested class Concept
Java Nested class ConceptJava Nested class Concept
Java Nested class Concept
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
java tutorial 4
 java tutorial 4 java tutorial 4
java tutorial 4
 

Similar to Java Inner Class

Session 21 - Inner Classes
Session 21 - Inner ClassesSession 21 - Inner Classes
Session 21 - Inner Classes
PawanMM
 
Inner Classes
Inner Classes Inner Classes
Inner Classes
Hitesh-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.ppt
RithwikRanjan
 
Java Programming inner and Nested classes.pptx
Java Programming inner and Nested classes.pptxJava Programming inner and Nested classes.pptx
Java Programming inner and Nested classes.pptx
AkashJha84
 
Nested class
Nested classNested class
Nested class
Daman Toor
 
Java Inner Classes
Java Inner ClassesJava Inner Classes
Java Inner Classes
Soba Arjun
 
Static Members-Java.pptx
Static Members-Java.pptxStatic Members-Java.pptx
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
Inner class
Inner classInner class
Inner class
Medivh2011
 
Developing Applications for Android - Lecture#2
Developing Applications for Android - Lecture#2Developing Applications for Android - Lecture#2
Developing Applications for Android - Lecture#2
Usman Chaudhry
 
Javasession8
Javasession8Javasession8
Javasession8
Rajeev Kumar
 
Inner classes9 cm604.28
Inner classes9 cm604.28Inner classes9 cm604.28
Inner classes9 cm604.28
myrajendra
 
Static Import and access modifiers
Static Import and access modifiersStatic Import and access modifiers
Static Import and access modifiers
Maitree Patel
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Anil Kumar
 
[Java] #8 String and Inner Class
[Java] #8 String and Inner Class[Java] #8 String and Inner Class
[Java] #8 String and Inner Class
Ghadeer AlHasan
 
03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop
Vladislav Hadzhiyski
 
Class&objects
Class&objectsClass&objects
Class&objects
harivng
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Sagar Verma
 
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
DaveEstonilo
 
Classes in Java great learning.pdf
Classes in Java great learning.pdfClasses in Java great learning.pdf
Classes in Java great learning.pdf
SHASHIKANT346021
 

Similar to Java Inner Class (19)

Session 21 - Inner Classes
Session 21 - Inner ClassesSession 21 - Inner Classes
Session 21 - Inner Classes
 
Inner Classes
Inner Classes Inner Classes
Inner Classes
 
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 Programming inner and Nested classes.pptx
Java Programming inner and Nested classes.pptxJava Programming inner and Nested classes.pptx
Java Programming inner and Nested classes.pptx
 
Nested class
Nested classNested class
Nested class
 
Java Inner Classes
Java Inner ClassesJava Inner Classes
Java Inner Classes
 
Static Members-Java.pptx
Static Members-Java.pptxStatic Members-Java.pptx
Static Members-Java.pptx
 
Inner class
Inner classInner class
Inner class
 
Developing Applications for Android - Lecture#2
Developing Applications for Android - Lecture#2Developing Applications for Android - Lecture#2
Developing Applications for Android - Lecture#2
 
Javasession8
Javasession8Javasession8
Javasession8
 
Inner classes9 cm604.28
Inner classes9 cm604.28Inner classes9 cm604.28
Inner classes9 cm604.28
 
Static Import and access modifiers
Static Import and access modifiersStatic Import and access modifiers
Static Import and access modifiers
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
[Java] #8 String and Inner Class
[Java] #8 String and Inner Class[Java] #8 String and Inner Class
[Java] #8 String and Inner Class
 
03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop
 
Class&objects
Class&objectsClass&objects
Class&objects
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
 
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
 
Classes in Java great learning.pdf
Classes in Java great learning.pdfClasses in Java great learning.pdf
Classes in Java great learning.pdf
 

More from DeeptiJava

Generating the Server Response: HTTP Status Codes
Generating the Server Response: HTTP Status CodesGenerating the Server Response: HTTP Status Codes
Generating the Server Response: HTTP Status Codes
DeeptiJava
 
Java Generics
Java GenericsJava Generics
Java Generics
DeeptiJava
 
Java Collection
Java CollectionJava Collection
Java Collection
DeeptiJava
 
Java Exception Handling
Java Exception HandlingJava Exception Handling
Java Exception Handling
DeeptiJava
 
Java OOPs
Java OOPs Java OOPs
Java OOPs
DeeptiJava
 
Java Access Specifier
Java Access SpecifierJava Access Specifier
Java Access Specifier
DeeptiJava
 
Java JDBC
Java JDBCJava JDBC
Java JDBC
DeeptiJava
 
Java Thread
Java ThreadJava Thread
Java Thread
DeeptiJava
 
JSP Part 2
JSP Part 2JSP Part 2
JSP Part 2
DeeptiJava
 
JSP Part 1
JSP Part 1JSP Part 1
JSP Part 1
DeeptiJava
 
Java I/O
Java I/OJava I/O
Java I/O
DeeptiJava
 
Java Hibernate Basics
Java Hibernate BasicsJava Hibernate Basics
Java Hibernate Basics
DeeptiJava
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
DeeptiJava
 

More from DeeptiJava (13)

Generating the Server Response: HTTP Status Codes
Generating the Server Response: HTTP Status CodesGenerating the Server Response: HTTP Status Codes
Generating the Server Response: HTTP Status Codes
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Java Collection
Java CollectionJava Collection
Java Collection
 
Java Exception Handling
Java Exception HandlingJava Exception Handling
Java Exception Handling
 
Java OOPs
Java OOPs Java OOPs
Java OOPs
 
Java Access Specifier
Java Access SpecifierJava Access Specifier
Java Access Specifier
 
Java JDBC
Java JDBCJava JDBC
Java JDBC
 
Java Thread
Java ThreadJava Thread
Java Thread
 
JSP Part 2
JSP Part 2JSP Part 2
JSP Part 2
 
JSP Part 1
JSP Part 1JSP Part 1
JSP Part 1
 
Java I/O
Java I/OJava I/O
Java I/O
 
Java Hibernate Basics
Java Hibernate BasicsJava Hibernate Basics
Java Hibernate Basics
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 

Recently uploaded

20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
Zilliz
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 

Recently uploaded (20)

20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 

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.