Reputation: 983
what is this mean?
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String location = GUI.Custom.QuickDialogs.selectFile(false);
try
{
PrintWriter pw = new PrintWriter(new File(location));
String text = textArea.getText();
pw.println(text);
pw.flush();
pw.close();
}
catch(Exception ex)
{
textArea.append("Could not save this debug output");
}
}
});
new ActionListener() {} what happened in {}? declare a method in the object?class? is ActionListener inner class?
Upvotes: 2
Views: 1416
Reputation: 2851
Have a look at:
http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
this syntax is anonymous class it is basically derived class which overrides the functionality, but as it is used only in this place probably there is no need of naming it.
They are pretty much created for same reasons as lambda statements, if something is being used only in one place of code why would you actually bother to name it, making the class or namespace bigger, polluting it.
Upvotes: 0
Reputation: 31699
The "anonymous inner class" is a common idiom for setting up things like listeners. However, in Java 8, since ActionListener
has only one method, it's now possible to use a lambda expression to do the same thing. The compiler will figure out that you're providing an implementation of actionPerformed
.
addActionListener((ActionEvent e) -> {
String location = GUI.Custom.QuickDialogs.selectFile(false);
try
{
PrintWriter pw = new PrintWriter(new File(location));
String text = textArea.getText();
pw.println(text);
pw.flush();
pw.close();
}
catch(Exception ex)
{
textArea.append("Could not save this debug output");
}
});
The first line could just be
addActionListener(e -> {
since the compiler will figure out that e
must be an ActionEvent
; a human reader might find it more difficult to spot it, though, depending on how familiar they are with AWT.
Upvotes: 0
Reputation: 8171
That's an anonymous inner class, a form of syntactic sugar to simplify writing single-use classes (often implementations of interfaces).
You can read more about them here.
Upvotes: 0
Reputation: 161
This is called anonymous class. Generally it's creating new implementation of ActionListener interface and overloading actionPerformed method.
Its equivalent of
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent e)
{
String location = GUI.Custom.QuickDialogs.selectFile(false);
...
}
}
addActionListener(new MyActionListener())
More information can be found at http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
Upvotes: 1
Reputation: 152304
With this statement you are creating new ActionListener
object with added new method actionPerformed
.
This is equal to creating new class which extends ActionListener
like:
class MyActionListener extends ActionListener {
public void actionPerformed(ActionEvent e) {
// ...
}
}
addActionListener(new MyActionListener());
Upvotes: 0
Reputation: 727097
The declarations inside curly braces that follow new ActionListener()
is the definition of an anonymous class that extends ActionListener
.
In your case, the anonymous class provides an implementation of a single method actionPerformed
. This feature lets you reduce the size of the code and bring declarations closer to the point where they are used in situations when you need a class that is used only from a single place in your code.
Upvotes: 7