Java Inner Classes
Upcoming SlideShare
Loading in...5
×
 

Java Inner Classes

on

  • 2,543 views

 

Statistics

Views

Total Views
2,543
Views on SlideShare
2,543
Embed Views
0

Actions

Likes
0
Downloads
59
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Java Inner Classes Java Inner Classes Presentation Transcript

  • Inner Classes Jussi Pohjolainen 1
  • Regular Inner Classclass Outer{! class Inner{ }!}!javac Outer.java!=>!Outer.class!Outer$Inner.class! 2
  • Running the inner-class•  You cant 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 View slide
  • Private members•  Inner class can have access to Outer classes private membersclass Outer {! private int x = 7;! class Inner {! public void seeOuter() {! System.out.println(x);! }! }!}! 4 View slide
  • 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 classclass 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
  • Exampleclass 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); // doesnt 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 Classclass 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