meljun cortes java lecture inner classes nested classes

Post on 13-May-2015

178 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

MELJUN CORTES Java Lecture Inner Classes Nested Classes

TRANSCRIPT

Inner Classes, Inner Classes, Anonymous Classes Anonymous Classes & Nested Classes& Nested Classes

Classes that belong to a class or a Classes that belong to a class or a methodmethod

MELJUN CORTESMELJUN CORTES

MELJUN CORTESMELJUN CORTES

What You Should LearnWhat You Should Learn

I.I. DefinitionsDefinitions

II.II. Why Create Inner/Anonymous/Nested Why Create Inner/Anonymous/Nested Classes?Classes?

III.III. Inner ClassesInner Classes

IV.IV. Method-Local ClassesMethod-Local Classes

V.V. Anonymous ClassesAnonymous Classes

VI.VI. Nested ClassesNested Classes

VII.VII. Best PracticeBest Practice

DefinitionsDefinitions

Inner Class Inner Class Class defined within another class.Class defined within another class. Instances have access to instance members of containing Instances have access to instance members of containing

class.class. Method-Local Inner ClassMethod-Local Inner Class

Class defined within a method.Class defined within a method. Only available within the method.Only available within the method.

Anonymous ClassesAnonymous Classes Classes with no class name!Classes with no class name! Defined during reference declaration.Defined during reference declaration.

Nested ClassNested Class Class defined within another class.Class defined within another class. Does not have access to instance members of containing Does not have access to instance members of containing

class.class.

Why Create Why Create Inner/Anonymous/Nested Classes?Inner/Anonymous/Nested Classes?

You have functionality that is specific to You have functionality that is specific to only one other class, but does not belong only one other class, but does not belong to that other class’s responsibility.to that other class’s responsibility.

Examples: Examples: Iterators for CollectionsIterators for Collections Event listeners for GUI classes.Event listeners for GUI classes.

Inner ClassesInner Classes

Defining an Inner ClassDefining an Inner Class Just declare the class within a classJust declare the class within a class

public class MyList {public class MyList { private Object [ ] a;private Object [ ] a; private int size;private int size; public MyIterator iterator() {public MyIterator iterator() {

return new MyIterator(); return new MyIterator(); } }

public class MyIterator {public class MyIterator { private int pos = 0;private int pos = 0; public boolean hasNext() { return pos < public boolean hasNext() { return pos < sizesize; } ; } public Object next()public Object next() { return { return aa[pos++]; }[pos++]; } }}}}

Inner ClassesInner Classes

Instantiating an Inner ClassInstantiating an Inner Class The outer class must exist first.The outer class must exist first.

MyList myList = new MyList();MyList myList = new MyList();

MyList.MyIterator iter MyList.MyIterator iter = = myList.new MyIterator();myList.new MyIterator();

Method-Local Inner ClassesMethod-Local Inner Classes

Defining a Method-Local Inner ClassDefining a Method-Local Inner Class Just declare the class within a methodJust declare the class within a method

void init() { class MyListener implements ActionListener { public void actionPerformed(ActionEvent e) { // some code here; } } ...}

Method-Local Inner ClassesMethod-Local Inner Classes

Instantiating a Method-Local Inner ClassInstantiating a Method-Local Inner Class Just instantiate as usual.Just instantiate as usual. You can only instantiate within the method.You can only instantiate within the method.

void init() { class MyListener implements ActionListener { ... } ActionListener listener = new MyListener(); this.addActionListner(listener); ...}

Anonymous ClassesAnonymous Classes

Sometimes, you don’t need to bother Sometimes, you don’t need to bother giving a class a name.giving a class a name.

Runnable car = new Runnable() { public void run() { while(true) { System.out.println(“VROOM!”); } }};

Anonymous ClassesAnonymous Classes

Anonymous classes subclass an existing Anonymous classes subclass an existing type.type.

Runnable car = new Runnable() { public void run() { while(true) { System.out.println(“VROOM!”); } }};

Anonymous ClassesAnonymous Classes

The type of the reference must be of an The type of the reference must be of an existing type.existing type.

Runnable car = new Runnable() { public void run() { while(true) { System.out.println(“VROOM!”); } }};

Anonymous ClassesAnonymous Classes

You can create the class as a field of the class.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 ClassesAnonymous Classes

Or more likely, you create it within a method or Or more likely, you create it within a method or constructor body.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 ClassesAnonymous Classes

Or you might define it while passing it as parameter!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 ClassesNested Classes

Defining an Nested ClassDefining an Nested Class Just like defining an inner class, but it must be static.Just like defining an inner class, but it must be static.

public class MyList { ... public MyIterator iterator() { ... }

public static class MyIterator { ... }}

Nested ClassesNested Classes

Nested classes cannot access instance members. 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 ClassesNested Classes

Instantiating an Nested ClassInstantiating an Nested Class No instance of outer class neededNo instance of outer class needed

MyList.MyIterator iter MyList.MyIterator iter = new MyList.MyIterator();= new MyList.MyIterator();

Best PracticeBest Practice

Use nested classes instead of inner Use nested classes instead of inner classes. classes. Direct access to members of outer class often Direct access to members of outer class often

gets confusing.gets confusing.

top related