SlideShare a Scribd company logo
Java Session 8
Inner Classes
• Inner classes let you define one class within another. They provide a type of scoping for your classes
since you can make one class a member of another class. Just as classes have member variables and
methods, a class can also have member classes.
• Different kinds of inner classes : Inner classes, Method-local inner classes, Anonymous inner classes,
Static nested classes.
• Regular Inner Classes :
• You're an OO programmer, so you know that for reuse and flexibility/extensibility you need to keep your classes
specialized. In other words, a class should have code only for the things an object of that particular type needs to do;
any other behavior should be part of another class better suited for that job. Sometimes, though, you find yourself
designing a class where you discover you need behavior that belongs in a separate, specialized class, but also needs to
be intimately tied to the class you're designing.
• One of the key benefits of an inner class is the "special relationship" an inner class instance shares with an instance of
the outer class. That "special relationship" gives code in the inner class access to members of the enclosing (outer)
class, as if the inner class were part of the outer class. In fact, that's exactly what it means: the inner class is a part of
the outer class. Not just a "part" but a full-fledged, card-carrying member of the outer class. Yes, an inner class
instance has access to all members of the outer class, even those marked private.
• Regular inner class can't have static declarations of any kind. The only way you can access the inner class is through a
live instance of the outer class!
• Instantiating an Inner Class : To create an instance of an inner class, you must have an instance of the outer class to
tie to the inner class. There are no exceptions to this rule: an inner class instance can never stand alone without a direct
relationship to an instance of the outer class
Regular Inner classes
• Referencing the Inner or Outer Instance from Within the Inner Class : How does an object refer
to itself normally? By using the this reference.
• The keyword this can be used only from within instance code. In other words, not within static code.
• The this reference is a reference to the currently executing object. In other words, the object whose reference was
used to invoke the currently running method.
• The this reference is the way an object can pass a reference to itself to some other code, as a method argument:
mc.doStuff(this);
• Within an inner class code, the this reference refers to the instance of the inner class, as you'd probably expect,
since this always refers to the currently executing object. But what if the inner class code wants an explicit
reference to the outer class instance that the inner instance is tied to? In other words, how do you reference the
"outer this"? Although normally the inner class code doesn't need a reference to the outer class, since it already has
an implicit one it's using to access the members of the outer class, it would need a reference to the outer class if it
needed to pass that reference to some other code as follows : MyOuter.this
• To reference the inner class instance itself, from within the inner class code, use this.
• To reference the "outer this" (the outer class instance) from within the inner class code, use
NameOfOuterClass.this (example, MyOuter.this).
• A regular inner class is a member of the outer class just as instance variables and methods are, so the
following modifiers can be applied to an inner class:
•final, abstract, public, private, protected,
• static—but static turns it into a static nested class not an inner class
•Strictfp
• Ex : https://gist.github.com/rajeevprasanna/10519350
Method-Local Inner classes
• A regular inner class is scoped inside another class's curly braces, but outside any method code (in other
words, at the same level that an instance variable is declared). But you can also define an inner class
within a method.
• A method-local inner class can be instantiated only within the method where the inner class is defined. In
other words, no other code running in any other method—inside or outside the outer class—can ever
instantiate the method-local inner class. Like regular inner class objects, the method-local inner class
object shares a special relationship with the enclosing (outer) class object, and can access its private (or
any other) members
• The inner class object cannot use the local variables of the method the inner class is in. Why not?
• The local variables of the method live on the stack, and exist only for the lifetime of the method. You
already know that the scope of a local variable is limited to the method the variable is declared in. When
the method ends, the stack frame is blown away and the variable is history. But even after the method
completes, the inner class object created within it might still be alive on the heap if, for example, a
reference to it was passed into some other code and then stored in an instance variable. Because the local
variables aren't guaranteed to be alive as long as the method-local inner class object, the inner class
object can't use them. Unless the local variables are marked final!
• The only modifiers you can apply to a method-local inner class are abstract and final, but as always,
never both at the same time.
• Local class declared in a static method has access to only static members of the enclosing class, since
there is no associated instance of the enclosing class. If you're in a static method there is no this, so an
inner class in a static method is subject to the same restrictions as the static method. In other words, no
access to instance variables.
• Ex : https://gist.github.com/rajeevprasanna/10519905
Anonymous Inner Classes
• An anonymous class is an inner class that does not have a name at all. And whose instance is being
created at the time of its creation.
• Creating anonymous class is quicker and simple. Anonymous inner classes are useful when we need to
inherit a few properties (only one method) of a superclass and this is not a good idea to take overhead of
creating a separate subclass for doing things so simple.
• In java anonymous classes can be created in either of the two ways. 1) Using a class reference variable.
2) Using an interface.
• Anonymous classes are just like local classes, besides they don’t have a name. In Java anonymous
classes enables the developer to declare and instantiate a class at the same time.
• Like other inner classes, an anonymous class has access to the members of its enclosing class.
• One more thing to keep in mind about anonymous interface implementers—they can implement only one
interface. There simply isn't any mechanism to say that your anonymous inner class is going to
implement multiple interfaces. In fact, an anonymous inner class can't even extend a class and implement
an interface at the same time.
• The inner class has to choose either to be a subclass of a named class— and not directly implement any
interfaces at all—or to implement a single interface. By directly, we mean actually using the keyword
implements as part of the class declaration.
• If the anonymous inner class is a subclass of a class type, it automatically becomes an implementer of
any interfaces implemented by the superclass.
• Ex : https://gist.github.com/rajeevprasanna/10520664
Static Nested Classes
• You'll sometimes hear static nested classes referred to as static inner classes, but they really aren't inner classes at all, by
the standard definition of an inner class.
• While an inner class (regardless of the flavor) enjoys that special relationship with the outer class (or rather the instances
of the two classes share a relationship), a static nested class does not. It is simply a non-inner (also called "top-level")
class scoped within another. So with static classes it's really more about name-space resolution than about an implicit
relationship between the two classes.
• The class itself isn't really "static"; there's no such thing as a static class. The static modifier in this case says that the
nested class is a static member of the outer class. That means it can be accessed, as with other static members, without
having an instance of the outer class.
• Just as a static method does not have access to the instance variables and nonstatic methods of the class, a
static nested class does not have access to the instance variables and nonstatic methods of the outer class.
Look for static nested classes with code that behaves like a nonstatic (regular inner) class.
• Ex: https://gist.github.com/rajeevprasanna/10520895
• Git repo URL of all examples : https://github.com/rajeevprasanna/JavaSession8
Javasession8

More Related Content

What's hot

Lecture 1 - Objects and classes
Lecture 1 - Objects and classesLecture 1 - Objects and classes
Lecture 1 - Objects and classes
Syed Afaq Shah MACS CP
 
15reflection in c#
15reflection  in c#15reflection  in c#
15reflection in c#
Sireesh K
 
Complete java&j2ee
Complete java&j2eeComplete java&j2ee
Complete java&j2eeShiva Cse
 
Packages
PackagesPackages
Packages
Nuha Noor
 
Inner classes9 cm604.28
Inner classes9 cm604.28Inner classes9 cm604.28
Inner classes9 cm604.28myrajendra
 
Introducing classes
Introducing classesIntroducing classes
Introducing classes
Riaz Ahmed
 
Pj01 x-classes and objects
Pj01 x-classes and objectsPj01 x-classes and objects
Pj01 x-classes and objects
SasidharaRaoMarrapu
 
Abstract classes & interfaces
Abstract classes & interfacesAbstract classes & interfaces
Abstract classes & interfaces
moazamali28
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
mcollison
 
GeekAustin PHP Class - Session 7
GeekAustin PHP Class - Session 7GeekAustin PHP Class - Session 7
GeekAustin PHP Class - Session 7jimbojsb
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Introduction to value types
Introduction to value typesIntroduction to value types
Introduction to value types
Narendran Solai Sridharan
 
OWL 2 Overview
OWL 2 OverviewOWL 2 Overview
OWL 2 Overview
Emiliano Reynares
 
Java introduction
Java introductionJava introduction
Java introduction
Muthukumaran Subramanian
 
JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O
Jyothishmathi Institute of Technology and Science Karimnagar
 
Java Core
Java CoreJava Core
Java Core
Gaurav Mehta
 
The Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael MaganaThe Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael Magana
Rafael Magana
 

What's hot (20)

Lecture 1 - Objects and classes
Lecture 1 - Objects and classesLecture 1 - Objects and classes
Lecture 1 - Objects and classes
 
15reflection in c#
15reflection  in c#15reflection  in c#
15reflection in c#
 
Complete java&j2ee
Complete java&j2eeComplete java&j2ee
Complete java&j2ee
 
Packages
PackagesPackages
Packages
 
Inner classes9 cm604.28
Inner classes9 cm604.28Inner classes9 cm604.28
Inner classes9 cm604.28
 
Introducing classes
Introducing classesIntroducing classes
Introducing classes
 
Unit 4
Unit 4Unit 4
Unit 4
 
Java Inner Classes
Java Inner ClassesJava Inner Classes
Java Inner Classes
 
Pj01 x-classes and objects
Pj01 x-classes and objectsPj01 x-classes and objects
Pj01 x-classes and objects
 
Class
ClassClass
Class
 
Abstract classes & interfaces
Abstract classes & interfacesAbstract classes & interfaces
Abstract classes & interfaces
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
 
GeekAustin PHP Class - Session 7
GeekAustin PHP Class - Session 7GeekAustin PHP Class - Session 7
GeekAustin PHP Class - Session 7
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Introduction to value types
Introduction to value typesIntroduction to value types
Introduction to value types
 
OWL 2 Overview
OWL 2 OverviewOWL 2 Overview
OWL 2 Overview
 
Java introduction
Java introductionJava introduction
Java introduction
 
JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O
 
Java Core
Java CoreJava Core
Java Core
 
The Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael MaganaThe Ruby Object Model by Rafael Magana
The Ruby Object Model by Rafael Magana
 

Similar to Javasession8

Nested classes in java
Nested classes in javaNested classes in java
Nested classes in java
ChiradipBhattacharya
 
Java Nested class Concept
Java Nested class ConceptJava Nested class Concept
Java Nested class Concept
jagriti srivastava
 
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
 
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
Hari kiran G
 
Lecture09.ppt
Lecture09.pptLecture09.ppt
Lecture09.ppt
hemanth248901
 
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
 
Nested class in java
Nested class in javaNested class in java
Nested class in java
ChiradipBhattacharya
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview Qestions
Arun Vasanth
 
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
AshwathGupta
 
Nested classes in java
Nested classes in javaNested classes in java
Nested classes in java
Richa Singh
 
Abstraction in java.pptx
Abstraction in java.pptxAbstraction in java.pptx
Abstraction in java.pptx
AsifMulani17
 
[圣思园][Java SE]Inner class
[圣思园][Java SE]Inner class[圣思园][Java SE]Inner class
[圣思园][Java SE]Inner classArBing Xie
 
Inner class
Inner classInner class
Inner class
Medivh2011
 
Classes in Java great learning.pdf
Classes in Java great learning.pdfClasses in Java great learning.pdf
Classes in Java great learning.pdf
SHASHIKANT346021
 
[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects
Muhammad Hammad Waseem
 
Class Members Access/Visibility Guide (Checklist)
Class Members Access/Visibility Guide (Checklist)Class Members Access/Visibility Guide (Checklist)
Class Members Access/Visibility Guide (Checklist)
Jayasree Perilakkalam
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
ethiouniverse
 
Nested class
Nested classNested class
Nested class
Daman Toor
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
HimanshuPandey957216
 

Similar to Javasession8 (20)

Nested classes in java
Nested classes in javaNested classes in java
Nested classes in java
 
Java Nested class Concept
Java Nested class ConceptJava Nested class Concept
Java Nested class Concept
 
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
 
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
 
Lecture09.ppt
Lecture09.pptLecture09.ppt
Lecture09.ppt
 
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
 
Inner class
Inner classInner class
Inner class
 
Nested class in java
Nested class in javaNested class in java
Nested class in java
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview Qestions
 
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
 
Nested classes in java
Nested classes in javaNested classes in java
Nested classes in java
 
Abstraction in java.pptx
Abstraction in java.pptxAbstraction in java.pptx
Abstraction in java.pptx
 
[圣思园][Java SE]Inner class
[圣思园][Java SE]Inner class[圣思园][Java SE]Inner class
[圣思园][Java SE]Inner class
 
Inner class
Inner classInner class
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
 
[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects
 
Class Members Access/Visibility Guide (Checklist)
Class Members Access/Visibility Guide (Checklist)Class Members Access/Visibility Guide (Checklist)
Class Members Access/Visibility Guide (Checklist)
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
 
Nested class
Nested classNested class
Nested class
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 

More from Rajeev Kumar

Db performance optimization with indexing
Db performance optimization with indexingDb performance optimization with indexing
Db performance optimization with indexing
Rajeev Kumar
 
Javasession10
Javasession10Javasession10
Javasession10
Rajeev Kumar
 
Java session 3
Java session 3Java session 3
Java session 3
Rajeev Kumar
 
Java session2
Java session2Java session2
Java session2
Rajeev Kumar
 

More from Rajeev Kumar (9)

Db performance optimization with indexing
Db performance optimization with indexingDb performance optimization with indexing
Db performance optimization with indexing
 
Javasession10
Javasession10Javasession10
Javasession10
 
Jms intro
Jms introJms intro
Jms intro
 
Javasession6
Javasession6Javasession6
Javasession6
 
Javasession7
Javasession7Javasession7
Javasession7
 
Javasession5
Javasession5Javasession5
Javasession5
 
Javasession4
Javasession4Javasession4
Javasession4
 
Java session 3
Java session 3Java session 3
Java session 3
 
Java session2
Java session2Java session2
Java session2
 

Recently uploaded

Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 

Recently uploaded (20)

Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 

Javasession8

  • 2. Inner Classes • Inner classes let you define one class within another. They provide a type of scoping for your classes since you can make one class a member of another class. Just as classes have member variables and methods, a class can also have member classes. • Different kinds of inner classes : Inner classes, Method-local inner classes, Anonymous inner classes, Static nested classes. • Regular Inner Classes : • You're an OO programmer, so you know that for reuse and flexibility/extensibility you need to keep your classes specialized. In other words, a class should have code only for the things an object of that particular type needs to do; any other behavior should be part of another class better suited for that job. Sometimes, though, you find yourself designing a class where you discover you need behavior that belongs in a separate, specialized class, but also needs to be intimately tied to the class you're designing. • One of the key benefits of an inner class is the "special relationship" an inner class instance shares with an instance of the outer class. That "special relationship" gives code in the inner class access to members of the enclosing (outer) class, as if the inner class were part of the outer class. In fact, that's exactly what it means: the inner class is a part of the outer class. Not just a "part" but a full-fledged, card-carrying member of the outer class. Yes, an inner class instance has access to all members of the outer class, even those marked private. • Regular inner class can't have static declarations of any kind. The only way you can access the inner class is through a live instance of the outer class! • Instantiating an Inner Class : To create an instance of an inner class, you must have an instance of the outer class to tie to the inner class. There are no exceptions to this rule: an inner class instance can never stand alone without a direct relationship to an instance of the outer class
  • 3. Regular Inner classes • Referencing the Inner or Outer Instance from Within the Inner Class : How does an object refer to itself normally? By using the this reference. • The keyword this can be used only from within instance code. In other words, not within static code. • The this reference is a reference to the currently executing object. In other words, the object whose reference was used to invoke the currently running method. • The this reference is the way an object can pass a reference to itself to some other code, as a method argument: mc.doStuff(this); • Within an inner class code, the this reference refers to the instance of the inner class, as you'd probably expect, since this always refers to the currently executing object. But what if the inner class code wants an explicit reference to the outer class instance that the inner instance is tied to? In other words, how do you reference the "outer this"? Although normally the inner class code doesn't need a reference to the outer class, since it already has an implicit one it's using to access the members of the outer class, it would need a reference to the outer class if it needed to pass that reference to some other code as follows : MyOuter.this • To reference the inner class instance itself, from within the inner class code, use this. • To reference the "outer this" (the outer class instance) from within the inner class code, use NameOfOuterClass.this (example, MyOuter.this). • A regular inner class is a member of the outer class just as instance variables and methods are, so the following modifiers can be applied to an inner class: •final, abstract, public, private, protected, • static—but static turns it into a static nested class not an inner class •Strictfp • Ex : https://gist.github.com/rajeevprasanna/10519350
  • 4. Method-Local Inner classes • A regular inner class is scoped inside another class's curly braces, but outside any method code (in other words, at the same level that an instance variable is declared). But you can also define an inner class within a method. • A method-local inner class can be instantiated only within the method where the inner class is defined. In other words, no other code running in any other method—inside or outside the outer class—can ever instantiate the method-local inner class. Like regular inner class objects, the method-local inner class object shares a special relationship with the enclosing (outer) class object, and can access its private (or any other) members • The inner class object cannot use the local variables of the method the inner class is in. Why not? • The local variables of the method live on the stack, and exist only for the lifetime of the method. You already know that the scope of a local variable is limited to the method the variable is declared in. When the method ends, the stack frame is blown away and the variable is history. But even after the method completes, the inner class object created within it might still be alive on the heap if, for example, a reference to it was passed into some other code and then stored in an instance variable. Because the local variables aren't guaranteed to be alive as long as the method-local inner class object, the inner class object can't use them. Unless the local variables are marked final! • The only modifiers you can apply to a method-local inner class are abstract and final, but as always, never both at the same time. • Local class declared in a static method has access to only static members of the enclosing class, since there is no associated instance of the enclosing class. If you're in a static method there is no this, so an inner class in a static method is subject to the same restrictions as the static method. In other words, no access to instance variables. • Ex : https://gist.github.com/rajeevprasanna/10519905
  • 5. Anonymous Inner Classes • An anonymous class is an inner class that does not have a name at all. And whose instance is being created at the time of its creation. • Creating anonymous class is quicker and simple. Anonymous inner classes are useful when we need to inherit a few properties (only one method) of a superclass and this is not a good idea to take overhead of creating a separate subclass for doing things so simple. • In java anonymous classes can be created in either of the two ways. 1) Using a class reference variable. 2) Using an interface. • Anonymous classes are just like local classes, besides they don’t have a name. In Java anonymous classes enables the developer to declare and instantiate a class at the same time. • Like other inner classes, an anonymous class has access to the members of its enclosing class. • One more thing to keep in mind about anonymous interface implementers—they can implement only one interface. There simply isn't any mechanism to say that your anonymous inner class is going to implement multiple interfaces. In fact, an anonymous inner class can't even extend a class and implement an interface at the same time. • The inner class has to choose either to be a subclass of a named class— and not directly implement any interfaces at all—or to implement a single interface. By directly, we mean actually using the keyword implements as part of the class declaration. • If the anonymous inner class is a subclass of a class type, it automatically becomes an implementer of any interfaces implemented by the superclass. • Ex : https://gist.github.com/rajeevprasanna/10520664
  • 6. Static Nested Classes • You'll sometimes hear static nested classes referred to as static inner classes, but they really aren't inner classes at all, by the standard definition of an inner class. • While an inner class (regardless of the flavor) enjoys that special relationship with the outer class (or rather the instances of the two classes share a relationship), a static nested class does not. It is simply a non-inner (also called "top-level") class scoped within another. So with static classes it's really more about name-space resolution than about an implicit relationship between the two classes. • The class itself isn't really "static"; there's no such thing as a static class. The static modifier in this case says that the nested class is a static member of the outer class. That means it can be accessed, as with other static members, without having an instance of the outer class. • Just as a static method does not have access to the instance variables and nonstatic methods of the class, a static nested class does not have access to the instance variables and nonstatic methods of the outer class. Look for static nested classes with code that behaves like a nonstatic (regular inner) class. • Ex: https://gist.github.com/rajeevprasanna/10520895 • Git repo URL of all examples : https://github.com/rajeevprasanna/JavaSession8