SlideShare uma empresa Scribd logo
1 de 7
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

Mais conteúdo relacionado

Mais procurados (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
 

Semelhante a Javasession8

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.pptxDaveEstonilo
 
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 CardHari kiran G
 
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.pptRithwikRanjan
 
Java/J2EE interview Qestions
Java/J2EE interview QestionsJava/J2EE interview Qestions
Java/J2EE interview QestionsArun 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.pptAshwathGupta
 
Nested classes in java
Nested classes in javaNested classes in java
Nested classes in javaRicha Singh
 
Abstraction in java.pptx
Abstraction in java.pptxAbstraction in java.pptx
Abstraction in java.pptxAsifMulani17
 
[圣思园][Java SE]Inner class
[圣思园][Java SE]Inner class[圣思园][Java SE]Inner class
[圣思园][Java SE]Inner classArBing Xie
 
Classes in Java great learning.pdf
Classes in Java great learning.pdfClasses in Java great learning.pdf
Classes in Java great learning.pdfSHASHIKANT346021
 
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
 

Semelhante a 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)
 
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
 
Lecture 10
Lecture 10Lecture 10
Lecture 10
 

Mais de Rajeev Kumar

Mais de 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
 

Último

ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 

Último (20)

ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 

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