Upload
meljun-cortes-mbampa
View
216
Download
0
Embed Size (px)
Citation preview
8/10/2019 MELJUN CORTES JAVA InnerAnonymousNestedClasses
1/18
Inner Classes,
Anonymous Classes
Nested Classes
Classes that belong to a class or a
method
MELJUN CORTES, MBA,MPA,BSCS
8/10/2019 MELJUN CORTES JAVA InnerAnonymousNestedClasses
2/18
8/10/2019 MELJUN CORTES JAVA InnerAnonymousNestedClasses
3/18
Definitions
Inner Class
Class defined within another class.
Instances have access to instance members of containingclass.
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 containingclass.
8/10/2019 MELJUN CORTES JAVA InnerAnonymousNestedClasses
4/18
Why Create
Inner/Anonymous/Nested
Classes?
You have functionality that is specific to
only one other class, but does not belong
to that other classs responsibility.
Examples:
Iterators for Collections
Event listeners for GUI classes.
8/10/2019 MELJUN CORTES JAVA InnerAnonymousNestedClasses
5/18
8/10/2019 MELJUN CORTES JAVA InnerAnonymousNestedClasses
6/18
Inner Classes
Instantiating an Inner Class
The outer class must exist first.
MyList myList = new MyList();
MyList.MyIterator iter= myList.new MyIterator );
8/10/2019 MELJUN CORTES JAVA InnerAnonymousNestedClasses
7/18
8/10/2019 MELJUN CORTES JAVA InnerAnonymousNestedClasses
8/18
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);
...
}
8/10/2019 MELJUN CORTES JAVA InnerAnonymousNestedClasses
9/18
8/10/2019 MELJUN CORTES JAVA InnerAnonymousNestedClasses
10/18
Anonymous Classes
Anonymous classes subclass an existing
type.
Runnable car = new Runnable() {
public void run() {
while(true) {
System.out.println(VROOM!);
}
}
};
8/10/2019 MELJUN CORTES JAVA InnerAnonymousNestedClasses
11/18
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!);}
}
};
8/10/2019 MELJUN CORTES JAVA InnerAnonymousNestedClasses
12/18
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);
}
}
8/10/2019 MELJUN CORTES JAVA InnerAnonymousNestedClasses
13/18
Anonymous Classes
Or more likely, you create it within a method orconstructor 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);
}}
8/10/2019 MELJUN CORTES JAVA InnerAnonymousNestedClasses
14/18
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) {
...
})
;
}
8/10/2019 MELJUN CORTES JAVA InnerAnonymousNestedClasses
15/18
Nested Classes
Defining an Nested Class
Just like defining an inner class, but it must be static.
public class MyList {...public MyIterator iterator() {
...}
public staticclass MyIterator {...}
}
8/10/2019 MELJUN CORTES JAVA InnerAnonymousNestedClasses
16/18
Nested Classes
Nested classes cannot access instance members.
public MyIterator iterator() {return new MyIterator(t s);
}
public staticclass MyIterator {private int pos = 0;private MyList
l st
;
private MyIterator MyList list) {
this.list = list;
}
public boolean hasNext() {
return pos < size;}public Object next() {
return l st.a[pos++];}
}
8/10/2019 MELJUN CORTES JAVA InnerAnonymousNestedClasses
17/18
Nested Classes
Instantiating an Nested Class
No instance of outer class needed
MyList.MyIterator iter= new MyList.MyIterator();
8/10/2019 MELJUN CORTES JAVA InnerAnonymousNestedClasses
18/18
Best Practice
Use nested classes instead of inner
classes.
Direct access to members of outer class often
gets confusing.