Java Inner Classes

2,251
-1

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,251
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
85
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java Inner Classes

  1. 1. Inner Classes Jussi Pohjolainen 1
  2. 2. Regular Inner Classclass Outer{! class Inner{ }!}!javac Outer.java!=>!Outer.class!Outer$Inner.class! 2
  3. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×