Escolar Documentos
Profissional Documentos
Cultura Documentos
Anonymous classes
2
Members of a class
• The definition of a class looks like:
3
Class = collection of members
4
Inner classes
5
Example
class OuterClass {
int myField;
// add some methods here
class InnerClass {
int anotherField;
int answer()
{
return (myField + anotherField);
}
}
}
7
Concrete example: a listener
public class SimpleGUI extends JFrame
{
JButton button;
JTextField output;
9
A nice design!
• This is a nice design:
– the listener class is genuinely part of the GUI
class, which makes sense
– there is no need to pass references around to
give the listener access to the GUI
components.
• Note that the GUI components are stored
in fields of the outer class, not local
variables in init().
10
Many nested inner classes…
11
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.
12
Scope of local variables
13
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.
14
Example
public void doSomething()
{
int localVariable;
15
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.
16
Concrete example
public class SimpleGUI extends JFrame {
JButton button;
JTextField output;
17
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 …
18
Points to note
19
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);
20
Anonymous values
• We would usually write:
System.out.println(Math.sqrt((x * x) + (y * y));
21
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.
22
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
}
}
23
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.
24
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.
25
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
});
26
Summary: inner classes
27
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.
28
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.
29
Exercise
30