Inner Classes,
Anonymous
Classes & Nested
Classes
MELJUN CORTES

Classes that belong to a class or a
method

MELJUN CORTES
What You Should
Learn
Definitions
II.
Why Create Inner/Anonymous/Nested
Classes?
III. Inner Classes
IV. Method-Local Class...
Definitions
 Inner Class

Class defined within another class.
 Instances have access to instance members of containing
c...
Why Create
Inner/Anonymous/Nested
Classes?
 You have functionality that is specific to

only one other class, but does no...
Inner Classes
 Defining an Inner Class
 Just declare the class within a class

public class MyList {
private Object [ ] ...
Inner Classes
 Instantiating an Inner Class


The outer class must exist first.

MyList myList = new MyList();
MyList.My...
Method-Local Inner
Classes
 Defining a Method-Local Inner Class
 Just declare the class within a method
void init() {
cl...
Method-Local Inner
Classes
 Instantiating a Method-Local Inner Class
 Just instantiate as usual.
 You can only instanti...
Anonymous Classes
 Sometimes, you don’t need to bother

giving a class a name.

Runnable car = new Runnable() {
public vo...
Anonymous Classes
 Anonymous classes subclass an existing

type.
Runnable car = new Runnable() {
public void run() {
whil...
Anonymous Classes
 The type of the reference must be of an

existing type.
Runnable car = new Runnable() {
public void ru...
Anonymous Classes
 You can create the class as a field of the class.

public class MyPanel extends JPanel {
private JButt...
Anonymous Classes
 Or more likely, you create it within a method or

constructor body.

public class MyPanel extends JPan...
Anonymous Classes
 Or you might define it while passing it as parameter!
private JButton b = new JButton();
public MyPane...
Nested Classes
 Defining an Nested Class
 Just like defining an inner class, but it must be static.

public class MyList...
Nested Classes
 Nested classes cannot access instance members.

public MyIterator iterator() {
return new MyIterator(this...
Nested Classes
 Instantiating an Nested Class
 No instance of outer class needed

MyList.MyIterator iter
= new MyList.My...
Best Practice
 Use nested classes instead of inner

classes.


Direct access to members of outer class often
gets confus...
Upcoming SlideShare
Loading in …5
×

MELJUN CORTES Java Lecture Inner Classes Nested Classes

448 views
261 views

Published on

MELJUN CORTES Java Lecture Inner Classes Nested Classes

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

  • Be the first to like this

No Downloads
Views
Total views
448
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

MELJUN CORTES Java Lecture Inner Classes Nested Classes

  1. 1. Inner Classes, Anonymous Classes & Nested Classes MELJUN CORTES Classes that belong to a class or a method MELJUN CORTES
  2. 2. What You Should Learn Definitions II. Why Create Inner/Anonymous/Nested Classes? III. Inner Classes IV. Method-Local Classes V. Anonymous Classes VI. Nested Classes VII. Best Practice I.
  3. 3. Definitions  Inner Class Class defined within another class.  Instances have access to instance members of containing class.  Method-Local Inner Class  Class defined within a method.  Only available within the method.  Anonymous Classes  Classes with no class name!  Defined during reference declaration.  Nested Class  Class defined within another class.  Does not have access to instance members of containing class. 
  4. 4. Why Create Inner/Anonymous/Nested Classes?  You have functionality that is specific to only one other class, but does not belong to that other class’s responsibility.  Examples: Iterators for Collections  Event listeners for GUI classes. 
  5. 5. Inner Classes  Defining an Inner Class  Just declare the class within a class public class MyList { private Object [ ] a; private int size; public MyIterator iterator() { return new MyIterator(); } public class MyIterator { private int pos = 0; public boolean hasNext() { return pos < size; } public Object next() { return a[pos++]; } } }
  6. 6. Inner Classes  Instantiating an Inner Class  The outer class must exist first. MyList myList = new MyList(); MyList.MyIterator iter = myList.new MyIterator();
  7. 7. Method-Local Inner Classes  Defining a Method-Local Inner Class  Just declare the class within a method void init() { class MyListener implements ActionListener { public void actionPerformed(ActionEvent e) { // some code here; } } ... }
  8. 8. Method-Local Inner Classes  Instantiating a Method-Local Inner Class  Just instantiate as usual.  You can only instantiate within the method. void init() { class MyListener implements ActionListener { ... } ActionListener listener = new MyListener(); this.addActionListner(listener); ... }
  9. 9. Anonymous Classes  Sometimes, you don’t need to bother giving a class a name. Runnable car = new Runnable() { public void run() { while(true) { System.out.println(“VROOM!”); } } };
  10. 10. Anonymous Classes  Anonymous classes subclass an existing type. Runnable car = new Runnable() { public void run() { while(true) { System.out.println(“VROOM!”); } } };
  11. 11. Anonymous Classes  The type of the reference must be of an existing type. Runnable car = new Runnable() { public void run() { while(true) { System.out.println(“VROOM!”); } } };
  12. 12. Anonymous Classes  You can create the class as a field of the class. public class MyPanel extends JPanel { private JButton b = new JButton(); private ActionListner l = new ActionListener() { public void actionPerformed(ActionEvent e) { ... } }; public MyPanel() { b.addActionListner(l); } }
  13. 13. Anonymous Classes  Or more likely, you create it within a method or constructor body. public class MyPanel extends JPanel { private JButton b = new JButton(); public MyPanel() { ActionListner l = new ActionListener() { public void actionPerformed(ActionEvent e) { ... } b.addActionListner(l); } }
  14. 14. Anonymous Classes  Or you might define it while passing it as parameter! private JButton b = new JButton(); public MyPanel() { b.addActionListner(new ActionListener() { public void actionPerformed(ActionEvent e) { ... }); }
  15. 15. Nested Classes  Defining an Nested Class  Just like defining an inner class, but it must be static. public class MyList { ... public MyIterator iterator() { ... } public static class MyIterator ... } } {
  16. 16. Nested Classes  Nested classes cannot access instance members. public MyIterator iterator() { return new MyIterator(this); } public static class MyIterator private int pos = 0; private MyList list; } { private MyIterator(MyList list) { this.list = list; } public boolean hasNext() { return pos < size; } public Object next() { return list.a[pos++]; }
  17. 17. Nested Classes  Instantiating an Nested Class  No instance of outer class needed MyList.MyIterator iter = new MyList.MyIterator();
  18. 18. Best Practice  Use nested classes instead of inner classes.  Direct access to members of outer class often gets confusing.

×