Your SlideShare is downloading. ×
Java Inner Classes
Java Inner Classes
Java Inner Classes
Java Inner Classes
Java Inner Classes
Java Inner Classes
Java Inner Classes
Java Inner Classes
Java Inner Classes
Java Inner Classes
Java Inner Classes
Java Inner Classes
Java Inner Classes
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Java Inner Classes

2,093

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,093
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
73
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Inner Classes Jussi Pohjolainen 1
  • 2. Regular Inner Classclass Outer{! class Inner{ }!}!javac Outer.java!=>!Outer.class!Outer$Inner.class! 2
  • 3. 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
  • 4. 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
  • 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 classclass 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. 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
  • 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 Classclass 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

×