• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
MELJUN CORTES Java Lecture Inner Classes Nested Classes
 

MELJUN CORTES Java Lecture Inner Classes Nested Classes

on

  • 244 views

MELJUN CORTES Java Lecture Inner Classes Nested Classes

MELJUN CORTES Java Lecture Inner Classes Nested Classes

Statistics

Views

Total Views
244
Views on SlideShare
244
Embed Views
0

Actions

Likes
0
Downloads
6
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

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

    MELJUN CORTES Java Lecture Inner Classes Nested Classes MELJUN CORTES Java Lecture Inner Classes Nested Classes Presentation Transcript

    • 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 Classes V. Anonymous Classes VI. Nested Classes VII. Best Practice I.
    • 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. 
    • 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. 
    • 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++]; } } }
    • Inner Classes  Instantiating an Inner Class  The outer class must exist first. MyList myList = new MyList(); MyList.MyIterator iter = myList.new MyIterator();
    • 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; } } ... }
    • 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); ... }
    • 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!”); } } };
    • Anonymous Classes  Anonymous classes subclass an existing type. Runnable car = new Runnable() { public void run() { while(true) { System.out.println(“VROOM!”); } } };
    • 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!”); } } };
    • 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); } }
    • 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); } }
    • 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) { ... }); }
    • 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 ... } } {
    • 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++]; }
    • Nested Classes  Instantiating an Nested Class  No instance of outer class needed MyList.MyIterator iter = new MyList.MyIterator();
    • Best Practice  Use nested classes instead of inner classes.  Direct access to members of outer class often gets confusing.