9
The listener itself
...
public class InnerListener implements ActionListener {
public void actionPerformed(ActionEvent e)
{
output.setText("Event handled");
}
}
...
more SimpleGUI code, if needed
}
12
Local variables
•
Recall that as well as fields (variables
which are members of a class), we have
local variables
, which live inside
methods, or smaller code-blocks.
public boolean isThisClear()
{
boolean answer = true;
return answer;
}
13
Scope of local variables
•
Local variables are only visible inside the
block (the { ... }) where they are declared.
•
They appear when they are declared, and
vanish again when the block is finished.
•
This is called their
scope
.
14
Local classes
•
We have seen
–
ordinary classes
–
inner classes, which are members of other
classes.
•
We can also have
local classes
, which live
inside a method body.
15
Example
public void doSomething()
{
int localVariable;
class LocalClass extends SomeClass {
// class definition here.
}
}
16
Local classes for listeners
•
It can sometimes be useful to use a local
class when writing a listener.
•
A local class has access to the fields and
methods of the main class, just like any
code in a method would do.
•
It also has access to other local variables
of the method, as long as they are declared
final
.
17
Concrete example
public class SimpleGUI extends JFrame {
JButton button;
JTextField output;
public void init() {
// set up a GUI
button = new JButton("press me");
...
18
Example continued
class LocalActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
output.setText("I handled the event");
}
}
button.addActionListener(new LocalActionListener());
... rest of init() method...
}
... rest of class ...
19
Points to note
•
Again the local class can access the fields
of the outer class directly, so there's no
need for that reference passing business.
•
The local class must be defined before it
is used! This is just like a local variable:
declare before using.
20
Anonymous values
•
In the previous example, we wrote a local class
which was only used once.
•
Usually, if we have some value that we only use
once, we try to avoid naming it. For instance,
we don't write:
int squareX = x * x;
int squareY = y * y;
double hypotenuse = Math.sqrt(squareX
+ squareY);
System.out.println(hypotenuse);
21
Anonymous values
•
We would usually write:
System.out.println(Math.sqrt((x * x) + (y * y));
•
Here the intermediate values are not stored
anywhere, so cannot be referred to by names.
That's okay because we don't need them again.
22
Anonymous classes
•
Java lets us have anonymous classes too.
•
An anonymous class is a class we will use just
once, "in passing", to create an object.
•
A common thing to use anonymous classes for
is listeners. Let's see how to create an
anonymous
ActionListener
class.
23
Anonymous class syntax
Here's how to write an anonymous class
and create an object of that class:
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
... code here
}
}
24
What it means
new ActionListener()
{
class definition
}
creates an object of an anonymous class
–
the anonymous class implements
ActionListener
–
the members of the class are contained within the
braces.
25
Another version
new SomeExistingClass()
{
class definition
}
creates an object of an anonymous class
–
the anonymous class
extends
SomeExistingClass
–
the members of the class are contained within the
braces.
26
Making use of the object
•
Of course, to use the object we've just created,
we need to store it somewhere, or do something
with it.
•
A common pattern is:
button.addActionListener(new ActionListener()
{
//
class definition
});
27
Summary: inner classes
•
Inner classes are written inside other
classes.
•
They are (usually) accessed only from
within the outer class.
•
They have access to the other fields and
methods of the outer class.
28
Summary: local classes
•
Local classes are classes within methods
or other code-blocks.
•
They can only be accessed from within
the code-block that contains them.
•
They have access to all the methods and
fields of the class containing the method.
•
They also have access to final local
variables of their containing method.
29
Summary: anonymous classes
•
Anonymous classes are local classes which have
no name and can only be used once, to create an
object.
•
An anonymous class must implement some
interface, or extend some other class. This
interface or class is used in the
new Something() {...}
syntax.
•
Otherwise, they are just like local classes.
30
Exercise
Convert any of your event-handling
programs so that the listeners are inner
classes, local classes, or anonymous
classes, as appropriate.



