SlideShare a Scribd company logo
Compose by
Adil Mehmood
Adilm717@gmail.com
What is Inner classes
Top-Level (or Outer) Class
You can put a class inside an another class.
 A class that contains other classes is a TLC.
Classe that you can write inside another class. Common applications include
iterators and GUIs
Syantax:
class Outer{
class Inner{ }
}
note:
there is no top level static class in java unlike C#
there is only inner static class
Nested Class
Nested class:
• Class declared inside another class.
Two kinds of nested classes:
• Member class: class declared at the
member-level of a TLC.
• Local class: class declared inside a
method, constructor, or initializer block
Inner Class
Inner class(IC) refers to two special kinds of nested class:
• Non-static member class (member class with no staticmodifier).
• Local class inside a non-static member of a TLC.
Why called inner class?
• Because an object made from the class will containa reference to the
TLC.
• Use TLC.this.memberfrom inside inner class to access member of
TLC.
Restrictions:
• Inner class fields can be static, but then must also be final.
• No staticmethods or other inner classes (same for other members?)
• See language references for even more details
Handy way to think of inner
classes inside a TLC
At the member level:
- just like a variable or method.
- called member class.
At the statement level:
- just like a statement in a method
- called local class
At the expression level:
- just like an expression
- called anonymous class
Member Class (Member Level)
Rules
Structure:
public class OuterClass{
tlc_members
public class InnerClass{
mc_members
}
}
When to use?
• The inner class generates objects used specifically by TLC.
• The inner class is associated with, or “connected to,” the TLC
How does visibility work?
• The inner class can be public, private, protected,
or package.
• Instances of the inner class type have
access to all members of the outer class
(including private and static members).
Some restrictions:
• Cannot have same name as TLC or package (not
that you would want to!).
• Cannot contain static members; can have static
finalfields (constants).
How do you use a member
class?
OuterClass oref = new OuterClass();
OuterClass.InnerClass iref = oref.new
InnerClass()
iref.doSomething();
new OuterClass().new InnerClass();
• Not valid:
InnerClass iref = new InnerClass();
iref.doSomething();
Internal references with this:
• Inside inner class, the this refers to current
instance of the inner class.
• To get to current instance of TLC, save the
TLC’s this as field in the TLC or
simply use TLC.this.
public class MemberClass {
public static void main(String[] args) {
// one way:
OC a = new OC();
OC.IC b = a.new IC();
b.print(); // outputs 3
// another way:
new OC().new IC().print(); // outputs 3
}
}
class OC {
private int x = 1;
public class IC {
private int y = 2;
public void print() {System.out.println(x+y);}
}
}
Local Classes (Statement Level)
Local class location:
• Statement level declaration.
• Usually written in methods. See also constructors
and initializers.
Scope:
• Local to block.
• Can access all members of the TLC.
• Actually, things can get confusing here!
- An object of local class might persist after
method ends.
- Java does have rules for dealing with the matter
More restrictions:
• Cannot be used outside of block.
• No modifiers.
• Enclosing block’s variables must be finalfor local
class to access.
• No static, but can have static final(constants).
• Terminate with a semicolon! The class is
effectively an expression statement.
• Cannot have same name of TLC
public class LocalClass {
public static void main(String[] args) {
new OC().print();
}
}
class OC {
public void print() {
final String s = "test: ";
class Point {
private int x;
private int y;
public Point(int x,int y) { this.x=x; this.y=y; }
public String toString() { return s+"("+x+","+y+")"; }
};
System.out.println(new Point(1,2));
} // method print
} //
Anonymous Class
Location and structure:
• Defined and created at expressionlevel.
• So, has no name and no modifiers.
Syntax:
new classname( argumentlist) { classbody}
new interfacename( argumentlist) { classbody}
Adapter class:
• Adapter class defines code that another object invokes.
• Common in GUIs and iterators.
Some restrictions:
• No modifiers.
• No static, but can have static final(constants).
• No constructors, but can use initializers for samepurpose! (See Section 1.2.)
When to use?
• Class has very short body.
• Only one instance of class needed.
• Class used right after defined; no need to create new class
In example below, we print a Pointagain. But, we cannot say new Point, because we
have not defined a Pointclass. Instead, I use a placeholder, class Object. You will often
find yourself using interface names instead.
public class AnonymousClass {
public static void main(String[] args) {
new OC().print();
}
}
class OC {
public void print() {
final String s = "test: ";
System.out.println(new Object() {
private int x=1;
private int y=2;
public String toString() { return s+"("+x+","+y+")"; }
} );
}
Instantiating an Inner class
Instantiating an Inner Class from within Outer
classes
code:
class Outer{!
private int x = 7;!
public void makeInner(){!
Inner x = new Inner();!
x.seeOuter();!
}!
class Inner{!
public void seeOuter(){!
System.out.println(x);!
}!
}!
}!
5
Creating an Inner Class from
Outside of the Outer class
You have to have instance of the
Outer-class
–  Outer outer = new Outer();
  After that, you create the Inner object
–  Outer.Inner inner = outer.new Inner() ;
• One Liner:
–  Outer.Inner inner = (new Outer()).new
Inner() ;
Method-local Inner Classes
Class inside a method
•  Can be instantiated only within the
method (below the class)
•  Can use Outer classes private
members
•  Cannot use methods variables!!
–  Unless the variable is final...
class Outer{!
private int x = 7;!
public void method(){!
final String y = "hi!";!
String z = "hi!";!
class Inner{!
public void seeOuter(){!
System.out.println(x); // works!!
System.out.println(y); // works!
//System.out.println(z); // doesn't work!
}!
}!
Inner object = new Inner();!
object.seeOuter();!
}!
}!

More Related Content

What's hot

L5 classes, objects, nested and inner class
L5 classes, objects, nested and inner classL5 classes, objects, nested and inner class
L5 classes, objects, nested and inner class
teach4uin
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
PhD Research Scholar
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in JavaRavi_Kant_Sahu
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
Prem Kumar Badri
 
Inner Classes
Inner ClassesInner Classes
Inner Classesparag
 
C# Access modifiers
C# Access modifiersC# Access modifiers
C# Access modifiers
Prem Kumar Badri
 
Nested classes in java
Nested classes in javaNested classes in java
Nested classes in java
ChiradipBhattacharya
 
Inner Classes in Java
Inner Classes in JavaInner Classes in Java
Inner Classes in Java
Dallington Asingwire
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Anil Kumar
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
Sohanur63
 
class c++
class c++class c++
class c++
vinay chauhan
 
Inner class
Inner classInner class
Inner class
Bansari Shah
 
Access specifier
Access specifierAccess specifier
Access specifier
zindadili
 
[圣思园][Java SE]Inner class
[圣思园][Java SE]Inner class[圣思园][Java SE]Inner class
[圣思园][Java SE]Inner classArBing Xie
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Lovely Professional University
 
Classes and objects in java
Classes and objects in javaClasses and objects in java
Classes and objects in java
Muthukumaran Subramanian
 
Java Nested class Concept
Java Nested class ConceptJava Nested class Concept
Java Nested class Concept
jagriti srivastava
 
Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasObject as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younas
Shahzad Younas
 
Write First C++ class
Write First C++ classWrite First C++ class
Write First C++ class
Learn By Watch
 

What's hot (20)

L5 classes, objects, nested and inner class
L5 classes, objects, nested and inner classL5 classes, objects, nested and inner class
L5 classes, objects, nested and inner class
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
 
Inner Classes
Inner ClassesInner Classes
Inner Classes
 
C# Access modifiers
C# Access modifiersC# Access modifiers
C# Access modifiers
 
Nested classes in java
Nested classes in javaNested classes in java
Nested classes in java
 
Inner Classes in Java
Inner Classes in JavaInner Classes in Java
Inner Classes in Java
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
 
class c++
class c++class c++
class c++
 
Inner class
Inner classInner class
Inner class
 
Access specifier
Access specifierAccess specifier
Access specifier
 
[圣思园][Java SE]Inner class
[圣思园][Java SE]Inner class[圣思园][Java SE]Inner class
[圣思园][Java SE]Inner class
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Classes and objects in java
Classes and objects in javaClasses and objects in java
Classes and objects in java
 
Java Nested class Concept
Java Nested class ConceptJava Nested class Concept
Java Nested class Concept
 
Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasObject as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younas
 
Write First C++ class
Write First C++ classWrite First C++ class
Write First C++ class
 

Viewers also liked

Inner classes9 cm604.28
Inner classes9 cm604.28Inner classes9 cm604.28
Inner classes9 cm604.28myrajendra
 
Character stream classes .52
Character stream classes .52Character stream classes .52
Character stream classes .52myrajendra
 
Byte stream classes.49
Byte stream classes.49Byte stream classes.49
Byte stream classes.49myrajendra
 
Files in java
Files in javaFiles in java
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47myrajendra
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
Tareq Hasan
 
Collections Framework
Collections FrameworkCollections Framework
Collections Framework
Sunil OS
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and Operators
Sunil OS
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
Sunil OS
 
Java Input Output (java.io.*)
Java Input Output (java.io.*)Java Input Output (java.io.*)
Java Input Output (java.io.*)
Om Ganesh
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streamsShahjahan Samoon
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Javabackdoor
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/Servlet
Sunil OS
 
Java Basics
Java BasicsJava Basics
Java Basics
Sunil OS
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
kamal kotecha
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
Anton Keks
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
Sunil OS
 
Java Swing
Java SwingJava Swing
Java Swing
Shraddha
 

Viewers also liked (20)

Inner classes9 cm604.28
Inner classes9 cm604.28Inner classes9 cm604.28
Inner classes9 cm604.28
 
Character stream classes .52
Character stream classes .52Character stream classes .52
Character stream classes .52
 
Byte stream classes.49
Byte stream classes.49Byte stream classes.49
Byte stream classes.49
 
Files in java
Files in javaFiles in java
Files in java
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
 
Collections Framework
Collections FrameworkCollections Framework
Collections Framework
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and Operators
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
 
Java Input Output (java.io.*)
Java Input Output (java.io.*)Java Input Output (java.io.*)
Java Input Output (java.io.*)
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Java
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/Servlet
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 
Java I/O
Java I/OJava I/O
Java I/O
 
Java swing
Java swingJava swing
Java swing
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
Java Swing
Java SwingJava Swing
Java Swing
 

Similar to Inner classes ,annoumous and outer classes in 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
 
Nested class
Nested classNested class
Nested class
Daman Toor
 
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
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
Tarunsingh198
 
Java tutorials
Java tutorialsJava tutorials
Java tutorialssaryu2011
 
JavaTutorials.ppt
JavaTutorials.pptJavaTutorials.ppt
JavaTutorials.ppt
Khizar40
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
Epsiba1
 
java tutorial 4
 java tutorial 4 java tutorial 4
java tutorial 4
Tushar Desarda
 
Lecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptxLecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptx
rayanbabur
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
Vibrant Technologies & Computers
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
DevaKumari Vijay
 
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
 
UNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year csUNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year cs
javed75
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
Lovely Professional University
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
Lovely Professional University
 

Similar to Inner classes ,annoumous and outer classes in java (20)

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
 
Nested class
Nested classNested class
Nested class
 
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
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 
Javasession8
Javasession8Javasession8
Javasession8
 
JavaTutorials.ppt
JavaTutorials.pptJavaTutorials.ppt
JavaTutorials.ppt
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
java tutorial 4
 java tutorial 4 java tutorial 4
java tutorial 4
 
Lecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptxLecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptx
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 
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
 
UNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year csUNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year cs
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 

More from Adil Mehmoood

Docker + Node "hello world" Application
Docker + Node "hello world" ApplicationDocker + Node "hello world" Application
Docker + Node "hello world" Application
Adil Mehmoood
 
Java database connectivity with MYSQL
Java database connectivity with MYSQLJava database connectivity with MYSQL
Java database connectivity with MYSQL
Adil Mehmoood
 
What is feasibility study and what is contracts and its type
What is feasibility study and what is contracts and its typeWhat is feasibility study and what is contracts and its type
What is feasibility study and what is contracts and its type
Adil Mehmoood
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
Adil Mehmoood
 
Project Engineer and Deputy project manger
Project Engineer and Deputy project manger Project Engineer and Deputy project manger
Project Engineer and Deputy project manger
Adil Mehmoood
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
Adil Mehmoood
 
Software Engineering 2 lecture slide
Software Engineering 2 lecture slideSoftware Engineering 2 lecture slide
Software Engineering 2 lecture slide
Adil Mehmoood
 
Expert system
Expert systemExpert system
Expert system
Adil Mehmoood
 
Sliding window and error control
Sliding window and error controlSliding window and error control
Sliding window and error control
Adil Mehmoood
 
Proposal defence format
Proposal defence formatProposal defence format
Proposal defence format
Adil Mehmoood
 
Shading and two type of shading flat shading and gauraud shading with coding ...
Shading and two type of shading flat shading and gauraud shading with coding ...Shading and two type of shading flat shading and gauraud shading with coding ...
Shading and two type of shading flat shading and gauraud shading with coding ...
Adil Mehmoood
 
Computer vesion
Computer vesionComputer vesion
Computer vesion
Adil Mehmoood
 
System quality attributes
System quality attributes System quality attributes
System quality attributes
Adil Mehmoood
 
Graph Clustering and cluster
Graph Clustering and clusterGraph Clustering and cluster
Graph Clustering and cluster
Adil Mehmoood
 
Token ring 802.5
Token ring 802.5Token ring 802.5
Token ring 802.5
Adil Mehmoood
 
diseases caused by computer
diseases caused by computerdiseases caused by computer
diseases caused by computer
Adil Mehmoood
 
Diseases caused by Computer
Diseases caused by ComputerDiseases caused by Computer
Diseases caused by ComputerAdil Mehmoood
 
What is Resume ,purpose and objective of resume and type of resume
What is Resume ,purpose and objective of resume and type of resumeWhat is Resume ,purpose and objective of resume and type of resume
What is Resume ,purpose and objective of resume and type of resume
Adil Mehmoood
 
Multiplexing and switching(TDM ,FDM, Data gram, circuit switching)
Multiplexing and switching(TDM ,FDM, Data gram, circuit switching)Multiplexing and switching(TDM ,FDM, Data gram, circuit switching)
Multiplexing and switching(TDM ,FDM, Data gram, circuit switching)
Adil Mehmoood
 

More from Adil Mehmoood (19)

Docker + Node "hello world" Application
Docker + Node "hello world" ApplicationDocker + Node "hello world" Application
Docker + Node "hello world" Application
 
Java database connectivity with MYSQL
Java database connectivity with MYSQLJava database connectivity with MYSQL
Java database connectivity with MYSQL
 
What is feasibility study and what is contracts and its type
What is feasibility study and what is contracts and its typeWhat is feasibility study and what is contracts and its type
What is feasibility study and what is contracts and its type
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Project Engineer and Deputy project manger
Project Engineer and Deputy project manger Project Engineer and Deputy project manger
Project Engineer and Deputy project manger
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
 
Software Engineering 2 lecture slide
Software Engineering 2 lecture slideSoftware Engineering 2 lecture slide
Software Engineering 2 lecture slide
 
Expert system
Expert systemExpert system
Expert system
 
Sliding window and error control
Sliding window and error controlSliding window and error control
Sliding window and error control
 
Proposal defence format
Proposal defence formatProposal defence format
Proposal defence format
 
Shading and two type of shading flat shading and gauraud shading with coding ...
Shading and two type of shading flat shading and gauraud shading with coding ...Shading and two type of shading flat shading and gauraud shading with coding ...
Shading and two type of shading flat shading and gauraud shading with coding ...
 
Computer vesion
Computer vesionComputer vesion
Computer vesion
 
System quality attributes
System quality attributes System quality attributes
System quality attributes
 
Graph Clustering and cluster
Graph Clustering and clusterGraph Clustering and cluster
Graph Clustering and cluster
 
Token ring 802.5
Token ring 802.5Token ring 802.5
Token ring 802.5
 
diseases caused by computer
diseases caused by computerdiseases caused by computer
diseases caused by computer
 
Diseases caused by Computer
Diseases caused by ComputerDiseases caused by Computer
Diseases caused by Computer
 
What is Resume ,purpose and objective of resume and type of resume
What is Resume ,purpose and objective of resume and type of resumeWhat is Resume ,purpose and objective of resume and type of resume
What is Resume ,purpose and objective of resume and type of resume
 
Multiplexing and switching(TDM ,FDM, Data gram, circuit switching)
Multiplexing and switching(TDM ,FDM, Data gram, circuit switching)Multiplexing and switching(TDM ,FDM, Data gram, circuit switching)
Multiplexing and switching(TDM ,FDM, Data gram, circuit switching)
 

Recently uploaded

Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
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
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
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
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
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
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
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
 
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
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 

Recently uploaded (20)

Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.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 Á...
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
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
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
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
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
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
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 

Inner classes ,annoumous and outer classes in java

  • 2. What is Inner classes Top-Level (or Outer) Class You can put a class inside an another class.  A class that contains other classes is a TLC. Classe that you can write inside another class. Common applications include iterators and GUIs Syantax: class Outer{ class Inner{ } } note: there is no top level static class in java unlike C# there is only inner static class
  • 3. Nested Class Nested class: • Class declared inside another class. Two kinds of nested classes: • Member class: class declared at the member-level of a TLC. • Local class: class declared inside a method, constructor, or initializer block
  • 4. Inner Class Inner class(IC) refers to two special kinds of nested class: • Non-static member class (member class with no staticmodifier). • Local class inside a non-static member of a TLC. Why called inner class? • Because an object made from the class will containa reference to the TLC. • Use TLC.this.memberfrom inside inner class to access member of TLC. Restrictions: • Inner class fields can be static, but then must also be final. • No staticmethods or other inner classes (same for other members?) • See language references for even more details
  • 5. Handy way to think of inner classes inside a TLC At the member level: - just like a variable or method. - called member class. At the statement level: - just like a statement in a method - called local class At the expression level: - just like an expression - called anonymous class
  • 6. Member Class (Member Level) Rules Structure: public class OuterClass{ tlc_members public class InnerClass{ mc_members } } When to use? • The inner class generates objects used specifically by TLC. • The inner class is associated with, or “connected to,” the TLC
  • 7. How does visibility work? • The inner class can be public, private, protected, or package. • Instances of the inner class type have access to all members of the outer class (including private and static members). Some restrictions: • Cannot have same name as TLC or package (not that you would want to!). • Cannot contain static members; can have static finalfields (constants).
  • 8. How do you use a member class? OuterClass oref = new OuterClass(); OuterClass.InnerClass iref = oref.new InnerClass() iref.doSomething(); new OuterClass().new InnerClass(); • Not valid: InnerClass iref = new InnerClass(); iref.doSomething();
  • 9. Internal references with this: • Inside inner class, the this refers to current instance of the inner class. • To get to current instance of TLC, save the TLC’s this as field in the TLC or simply use TLC.this.
  • 10. public class MemberClass { public static void main(String[] args) { // one way: OC a = new OC(); OC.IC b = a.new IC(); b.print(); // outputs 3 // another way: new OC().new IC().print(); // outputs 3 } } class OC { private int x = 1; public class IC { private int y = 2; public void print() {System.out.println(x+y);} } }
  • 11. Local Classes (Statement Level) Local class location: • Statement level declaration. • Usually written in methods. See also constructors and initializers. Scope: • Local to block. • Can access all members of the TLC. • Actually, things can get confusing here! - An object of local class might persist after method ends. - Java does have rules for dealing with the matter
  • 12. More restrictions: • Cannot be used outside of block. • No modifiers. • Enclosing block’s variables must be finalfor local class to access. • No static, but can have static final(constants). • Terminate with a semicolon! The class is effectively an expression statement. • Cannot have same name of TLC
  • 13. public class LocalClass { public static void main(String[] args) { new OC().print(); } } class OC { public void print() { final String s = "test: "; class Point { private int x; private int y; public Point(int x,int y) { this.x=x; this.y=y; } public String toString() { return s+"("+x+","+y+")"; } }; System.out.println(new Point(1,2)); } // method print } //
  • 14. Anonymous Class Location and structure: • Defined and created at expressionlevel. • So, has no name and no modifiers. Syntax: new classname( argumentlist) { classbody} new interfacename( argumentlist) { classbody} Adapter class: • Adapter class defines code that another object invokes. • Common in GUIs and iterators. Some restrictions: • No modifiers. • No static, but can have static final(constants). • No constructors, but can use initializers for samepurpose! (See Section 1.2.) When to use? • Class has very short body. • Only one instance of class needed. • Class used right after defined; no need to create new class
  • 15. In example below, we print a Pointagain. But, we cannot say new Point, because we have not defined a Pointclass. Instead, I use a placeholder, class Object. You will often find yourself using interface names instead. public class AnonymousClass { public static void main(String[] args) { new OC().print(); } } class OC { public void print() { final String s = "test: "; System.out.println(new Object() { private int x=1; private int y=2; public String toString() { return s+"("+x+","+y+")"; } } ); }
  • 16. Instantiating an Inner class Instantiating an Inner Class from within Outer classes code: class Outer{! private int x = 7;! public void makeInner(){! Inner x = new Inner();! x.seeOuter();! }! class Inner{! public void seeOuter(){! System.out.println(x);! }! }! }! 5
  • 17. Creating an Inner Class from Outside of the Outer class You have to have instance of the Outer-class –  Outer outer = new Outer();   After that, you create the Inner object –  Outer.Inner inner = outer.new Inner() ; • One Liner: –  Outer.Inner inner = (new Outer()).new Inner() ;
  • 18. Method-local Inner Classes Class inside a method •  Can be instantiated only within the method (below the class) •  Can use Outer classes private members •  Cannot use methods variables!! –  Unless the variable is final...
  • 19. class Outer{! private int x = 7;! public void method(){! final String y = "hi!";! String z = "hi!";! class Inner{! public void seeOuter(){! System.out.println(x); // works!! System.out.println(y); // works! //System.out.println(z); // doesn't work! }! }! Inner object = new Inner();! object.seeOuter();! }! }!