Reputation: 2556
I'm new to java and I'm still trying to understand the language, so I apologize if this question might sound noobish.
There's something I don't understand about listeners, sometime you can see a listener declared in such a fashion:
private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {
// Some code
}
};
or:
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
The thing that perplex me the most is that semicolon and parenthesis after the end of the method.
I understand inner classes for the purpose of having multiple listeners, but I don't understand this mixed declaration of variables and method.
Which purpose it has?
How is it declared?
WTF? :P
cheers :)
Upvotes: 3
Views: 1970
Reputation: 87513
Normally defining a class, instantiating an instance, and then using that instance are done separately:
class MyListener extends OnClickListener {
public void onClick(View v) {
// my onClick code goes here
}
}
MyListener foo = new MyListener();
button.setOnClickListener(foo);
But sometimes you need a subclass that you will instantiate only once immediately, which is often the case for event handlers. It is convenient to define it and instantiate it together using an anonymous (inner) class:
OnClickListener foo =
new OnClickListener() {
public void onClick(View v) {
// my onClick code goes here
}
};
button.setOnClickListener(foo);
But since foo
is only used once, we can takes this one step further and eliminate the local variable foo
as well, so:
button.setOnClickListener(foo);
can be formatted as:
button.setOnClickListener(
foo
);
substitute in the value of foo
:
button.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
// my onClick code goes here
}
}
);
reformatted again without some much whitespace to see it as it is often written:
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// my onClick code goes here
}
});
I think this last format hurts readability. I format my anonymous classes similar to how it is in the next to last format - the better readability (IMHO) is worth a little extra whitespace.
Upvotes: 3
Reputation: 24630
That code applies anonymous classes. Search and read for this topic.
The semicolon terminates the assignment respectively the method invocation.
PS. I always was thinking the name is something misleading because what you really get is an Object.
Upvotes: 1
Reputation: 308743
Break it down...
javax.swing.SwingUtilities.invokeLater(Runnable runnable);
invokeLater()
is a static method on SwingUtilities
that takes a Runnable
as a parameter.
Note the right parenthesis and semi-colon at the end; it's just a standard method, right?
The stuff in the middle is just creating a new instance of a Runnable to pass to the method. You're recognize it better if I assigned it to a reference:
Runnable parameter = new Runnable()
{
public void run()
{
createAndShowGUI();
}
}
};
Upvotes: 2
Reputation: 40168
There's nothing perplexing about the semicolon or parentheses here. Basically, this is one way to assign an anonymous class in Java. If you are not used to this construct, you can certainly create your own class that implements Runnable, and assign it like this:-
public class YourRunnableClass implements Runnable {
public void run() {
...
}
}
Then you can make it look nicer, like this:-
javax.swing.SwingUtilities.invokeLater(new YourRunnableClass());
P/S: Try not to use "WTF" in your post. :)
Upvotes: 2
Reputation: 5406
I'll try to exemplify the second example.
In this case invokeLater method expects any object which implements the Runnable
interface.
for example, this is an alternative way of writing the same thing without an anonymous class:
class Example implements Runnable{
public void run(){
// do something
}
}
and your example could be:
javax.swing.SwingUtilities.invokeLater(new Example());
But since this Example class it's likely you'll use it only once, it's more convenient to use an anonymous class.
Upvotes: 1
Reputation: 4017
That is an anonymous inner class. It's a very common and handy construct. Read the tutorial I linked to!
Upvotes: 1