SlideShare a Scribd company logo
Inner Classes
 Jussi Pohjolainen




                     1
Regular Inner Class
class Outer{!
  class Inner{ }!
}!
javac Outer.java!
=>!
Outer.class!
Outer$Inner.class!

                           2
Running the inner-class
•  You can't access the Inner class in a
   usual way
  –  java Outer$Inner
  –  because regular Inner-class cannot
     contain main-method. It cannot contain
     any static content!



                                         3
Private members
•  Inner class can have access to Outer
   classes private members
class Outer {!
     private int x = 7;!
     class Inner {!
          public void seeOuter() {!
             System.out.println(x);!
          }!
     }!
}!                                     4
Instantiating an Inner class
•  To instantiate: You must have an instance of the outer
   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()

    Class Class          Object   new Object

•  One Liner:
  –  Outer.Inner inner = (new Outer()).new Inner()
                                             6
Referencing Inner / Outer
    class from Inner class
class Outer{
   class Inner{
      public void seeOuter(){
        System.out.println(this);
        System.out.println(Outer.this);
      }
   }
}                                     7
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...

                                         8
Example
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();!
    }!
}!
                                                    9
Anonymous Inner Classes
    class Popcorn {!
        public void pop() {!
            System.out.println("popcorn");!
        }!
    }!                                  Subclass
    !                                      of
    class Food {!                       Popcorn!
        Popcorn p = new Popcorn() {!
            public void pop() {!
superclass!    System.out.println("subclass Popcorn!");!
            }!
        };!       Notice the semicolon!
    !
    }!
                                                   10
Anonymous Inner classes
          (Interface)
interface Cookable {!
   public void cook();!
}!
!
class Food {!               Implementation of the Interface
   Cookable p = new Cookable() {!
      public void cook() {!
         System.out.println("Cook
         implementer");!
      }!
   };!
}!
                                               11
Argument-Defined Anonymous
        Inner Class
class MyClass{!
   void go(){!
      Bar b = new Bar();!
      b.doStuff(new Foo() {!
         public void foof() {!
            System.out.println("foofy");!
         }!
      });!
   }!
}!
!                                        12
Static Nested Classes
•  Static member of the enclosing class.
•  class BigOuter{
  –  static class Nested { }
•  }
•  Does not have access to the instance
   variables!
•  BigOuter.Nested n = new
   BigOuter.Nested()
                                     13

More Related Content

PPTX
Java- Nested Classes
Prabhdeep Singh
 
PPT
Inner classes ,annoumous and outer classes in java
Adil Mehmoood
 
DOCX
Nested classes in java
Richa Singh
 
DOCX
Nested class in java
ChiradipBhattacharya
 
PPTX
Inner classes
DraftKing Zohaib
 
PPTX
Java Inner Class
DeeptiJava
 
PPTX
Inner class
Guna Sekaran
 
PPTX
Inner Classes & Multi Threading in JAVA
Tech_MX
 
Java- Nested Classes
Prabhdeep Singh
 
Inner classes ,annoumous and outer classes in java
Adil Mehmoood
 
Nested classes in java
Richa Singh
 
Nested class in java
ChiradipBhattacharya
 
Inner classes
DraftKing Zohaib
 
Java Inner Class
DeeptiJava
 
Inner class
Guna Sekaran
 
Inner Classes & Multi Threading in JAVA
Tech_MX
 

What's hot (18)

PPT
L5 classes, objects, nested and inner class
teach4uin
 
PPTX
Module 11 : Inheritance
Prem Kumar Badri
 
PDF
Classes and Nested Classes in Java
Ravi_Kant_Sahu
 
PPT
Inner classes9 cm604.28
myrajendra
 
PPTX
Java class,object,method introduction
Sohanur63
 
PPTX
Inner classes
Icancode
 
PPT
Inner Classes
parag
 
PDF
Java Inner Classes
Soba Arjun
 
PDF
Inner Classes in Java
Dallington Asingwire
 
PPTX
Pj01 x-classes and objects
SasidharaRaoMarrapu
 
PPTX
Unit 5 java-awt (1)
DevaKumari Vijay
 
PDF
[圣思园][Java SE]Inner class
ArBing Xie
 
PPTX
Classes and objects
Anil Kumar
 
PPTX
class c++
vinay chauhan
 
PPTX
Unit3 part2-inheritance
DevaKumari Vijay
 
PPTX
Classes
Sheheryar Gull
 
PPTX
Write First C++ class
Learn By Watch
 
PDF
Object oriented programming With C#
Youssef Mohammed Abohaty
 
L5 classes, objects, nested and inner class
teach4uin
 
Module 11 : Inheritance
Prem Kumar Badri
 
Classes and Nested Classes in Java
Ravi_Kant_Sahu
 
Inner classes9 cm604.28
myrajendra
 
Java class,object,method introduction
Sohanur63
 
Inner classes
Icancode
 
Inner Classes
parag
 
Java Inner Classes
Soba Arjun
 
Inner Classes in Java
Dallington Asingwire
 
Pj01 x-classes and objects
SasidharaRaoMarrapu
 
Unit 5 java-awt (1)
DevaKumari Vijay
 
[圣思园][Java SE]Inner class
ArBing Xie
 
Classes and objects
Anil Kumar
 
class c++
vinay chauhan
 
Unit3 part2-inheritance
DevaKumari Vijay
 
Write First C++ class
Learn By Watch
 
Object oriented programming With C#
Youssef Mohammed Abohaty
 
Ad

Viewers also liked (20)

PPS
Introduction to class in java
kamal kotecha
 
PPTX
Classes, objects in JAVA
Abhilash Nair
 
PPTX
Java static keyword
Ahmed Shawky El-faky
 
PPT
Byte stream classes.49
myrajendra
 
PPT
Character stream classes .52
myrajendra
 
PDF
Files in java
Muthukumaran Subramanian
 
PPT
Various io stream classes .47
myrajendra
 
PPT
Java: Objects and Object References
Tareq Hasan
 
PPT
Collections Framework
Sunil OS
 
PPT
JAVA Variables and Operators
Sunil OS
 
PPT
JAVA OOP
Sunil OS
 
PDF
Exception handling
Ravi Kant Sahu
 
PPTX
Java Input Output (java.io.*)
Om Ganesh
 
PDF
Event handling
Ravi Kant Sahu
 
PPTX
Understanding java streams
Shahjahan Samoon
 
PPT
Object and Classes in Java
backdoor
 
PPT
Jsp/Servlet
Sunil OS
 
PPTX
Java bytecode and classes
yoavwix
 
PPT
Java Basics
Sunil OS
 
PDF
The visual interface is now your brand
Nick Myers
 
Introduction to class in java
kamal kotecha
 
Classes, objects in JAVA
Abhilash Nair
 
Java static keyword
Ahmed Shawky El-faky
 
Byte stream classes.49
myrajendra
 
Character stream classes .52
myrajendra
 
Various io stream classes .47
myrajendra
 
Java: Objects and Object References
Tareq Hasan
 
Collections Framework
Sunil OS
 
JAVA Variables and Operators
Sunil OS
 
JAVA OOP
Sunil OS
 
Exception handling
Ravi Kant Sahu
 
Java Input Output (java.io.*)
Om Ganesh
 
Event handling
Ravi Kant Sahu
 
Understanding java streams
Shahjahan Samoon
 
Object and Classes in Java
backdoor
 
Jsp/Servlet
Sunil OS
 
Java bytecode and classes
yoavwix
 
Java Basics
Sunil OS
 
The visual interface is now your brand
Nick Myers
 
Ad

Similar to Java Inner Classes (20)

PPTX
WINSEMFRE2024-25_CSE2005_ETH_AP2024255000715_2025-03-18_Reference-Material-I....
belgiumsckgr
 
PPTX
Java Nested classes, static class and methods, nested blocks_Inner_Classes.pptx
Satyanandaram Nandigam
 
PPTX
Java Programming inner and Nested classes.pptx
AkashJha84
 
PDF
Basic java for Android Developer
Nattapong Tonprasert
 
PPT
A1771937735_21789_14_2018__16_ Nested Classes.ppt
RithwikRanjan
 
ODP
Synapseindia reviews.odp.
Tarunsingh198
 
PDF
LECTURE 7 REVIEW, EXCEPTIONS, IO.pdf
ShashikantSathe3
 
PPTX
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
JeevaR43
 
PPT
JavaTutorials.ppt
Khizar40
 
PPT
core java
Vinodh Kumar
 
PPT
Java tutorials
saryu2011
 
PPT
Java
s4al_com
 
PDF
Inheritance
Sardar Alam
 
PPTX
Session 21 - Inner Classes
PawanMM
 
PPTX
Anonymous classes2
Mark Baker
 
PDF
Java OO Revisited
Jussi Pohjolainen
 
PPTX
Turbinando o compilador do Java 8
Marcelo de Castro
 
PPTX
Logic and Coding of Java Interfaces & Swing Applications
kjkleindorfer
 
PPTX
Super Keyword in Java.pptx
KrutikaWankhade1
 
WINSEMFRE2024-25_CSE2005_ETH_AP2024255000715_2025-03-18_Reference-Material-I....
belgiumsckgr
 
Java Nested classes, static class and methods, nested blocks_Inner_Classes.pptx
Satyanandaram Nandigam
 
Java Programming inner and Nested classes.pptx
AkashJha84
 
Basic java for Android Developer
Nattapong Tonprasert
 
A1771937735_21789_14_2018__16_ Nested Classes.ppt
RithwikRanjan
 
Synapseindia reviews.odp.
Tarunsingh198
 
LECTURE 7 REVIEW, EXCEPTIONS, IO.pdf
ShashikantSathe3
 
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
JeevaR43
 
JavaTutorials.ppt
Khizar40
 
core java
Vinodh Kumar
 
Java tutorials
saryu2011
 
Java
s4al_com
 
Inheritance
Sardar Alam
 
Session 21 - Inner Classes
PawanMM
 
Anonymous classes2
Mark Baker
 
Java OO Revisited
Jussi Pohjolainen
 
Turbinando o compilador do Java 8
Marcelo de Castro
 
Logic and Coding of Java Interfaces & Swing Applications
kjkleindorfer
 
Super Keyword in Java.pptx
KrutikaWankhade1
 

More from Jussi Pohjolainen (20)

PDF
Moved to Speakerdeck
Jussi Pohjolainen
 
PDF
Java Web Services
Jussi Pohjolainen
 
PDF
Box2D and libGDX
Jussi Pohjolainen
 
PDF
libGDX: Screens, Fonts and Preferences
Jussi Pohjolainen
 
PDF
libGDX: Tiled Maps
Jussi Pohjolainen
 
PDF
libGDX: User Input and Frame by Frame Animation
Jussi Pohjolainen
 
PDF
Intro to Building Android Games using libGDX
Jussi Pohjolainen
 
PDF
Advanced JavaScript Development
Jussi Pohjolainen
 
PDF
Introduction to JavaScript
Jussi Pohjolainen
 
PDF
Introduction to AngularJS
Jussi Pohjolainen
 
PDF
libGDX: Scene2D
Jussi Pohjolainen
 
PDF
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
PDF
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
PDF
libGDX: User Input
Jussi Pohjolainen
 
PDF
Implementing a Simple Game using libGDX
Jussi Pohjolainen
 
PDF
Building Android games using LibGDX
Jussi Pohjolainen
 
PDF
Android Threading
Jussi Pohjolainen
 
PDF
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Jussi Pohjolainen
 
PDF
Creating Games for Asha - platform
Jussi Pohjolainen
 
PDF
Intro to Asha UI
Jussi Pohjolainen
 
Moved to Speakerdeck
Jussi Pohjolainen
 
Java Web Services
Jussi Pohjolainen
 
Box2D and libGDX
Jussi Pohjolainen
 
libGDX: Screens, Fonts and Preferences
Jussi Pohjolainen
 
libGDX: Tiled Maps
Jussi Pohjolainen
 
libGDX: User Input and Frame by Frame Animation
Jussi Pohjolainen
 
Intro to Building Android Games using libGDX
Jussi Pohjolainen
 
Advanced JavaScript Development
Jussi Pohjolainen
 
Introduction to JavaScript
Jussi Pohjolainen
 
Introduction to AngularJS
Jussi Pohjolainen
 
libGDX: Scene2D
Jussi Pohjolainen
 
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
libGDX: Simple Frame Animation
Jussi Pohjolainen
 
libGDX: User Input
Jussi Pohjolainen
 
Implementing a Simple Game using libGDX
Jussi Pohjolainen
 
Building Android games using LibGDX
Jussi Pohjolainen
 
Android Threading
Jussi Pohjolainen
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Jussi Pohjolainen
 
Creating Games for Asha - platform
Jussi Pohjolainen
 
Intro to Asha UI
Jussi Pohjolainen
 

Recently uploaded (20)

PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PPT
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Chapter 1 Introduction to CV and IP Lecture Note.pdf
Getnet Tigabie Askale -(GM)
 
PDF
Software Development Company | KodekX
KodekX
 
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
Doc9.....................................
SofiaCollazos
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Chapter 1 Introduction to CV and IP Lecture Note.pdf
Getnet Tigabie Askale -(GM)
 
Software Development Company | KodekX
KodekX
 
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 

Java Inner Classes

  • 1. Inner Classes Jussi Pohjolainen 1
  • 2. Regular Inner Class class Outer{! class Inner{ }! }! javac Outer.java! =>! Outer.class! Outer$Inner.class! 2
  • 3. Running the inner-class •  You can't access the Inner class in a usual way –  java Outer$Inner –  because regular Inner-class cannot contain main-method. It cannot contain any static content! 3
  • 4. Private members •  Inner class can have access to Outer classes private members class Outer {! private int x = 7;! class Inner {! public void seeOuter() {! System.out.println(x);! }! }! }! 4
  • 5. Instantiating an Inner class •  To instantiate: You must have an instance of the outer 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 }!
  • 6. 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() Class Class Object new Object •  One Liner: –  Outer.Inner inner = (new Outer()).new Inner() 6
  • 7. Referencing Inner / Outer class from Inner class class Outer{ class Inner{ public void seeOuter(){ System.out.println(this); System.out.println(Outer.this); } } } 7
  • 8. 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... 8
  • 9. Example 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();! }! }! 9
  • 10. Anonymous Inner Classes class Popcorn {! public void pop() {! System.out.println("popcorn");! }! }! Subclass ! of class Food {! Popcorn! Popcorn p = new Popcorn() {! public void pop() {! superclass! System.out.println("subclass Popcorn!");! }! };! Notice the semicolon! ! }! 10
  • 11. Anonymous Inner classes (Interface) interface Cookable {! public void cook();! }! ! class Food {! Implementation of the Interface Cookable p = new Cookable() {! public void cook() {! System.out.println("Cook implementer");! }! };! }! 11
  • 12. Argument-Defined Anonymous Inner Class class MyClass{! void go(){! Bar b = new Bar();! b.doStuff(new Foo() {! public void foof() {! System.out.println("foofy");! }! });! }! }! ! 12
  • 13. Static Nested Classes •  Static member of the enclosing class. •  class BigOuter{ –  static class Nested { } •  } •  Does not have access to the instance variables! •  BigOuter.Nested n = new BigOuter.Nested() 13