Below code snippet is from the website of smartgwt. While calling the method addButtonClickHandler implementation of the interface ButtonClickHandler is passed as anonymous object.What is this concept called in Java?
final Dialog dialog = new Dialog();
dialog.setMessage("Please choose whether to proceed");
dialog.setIcon("[SKIN]ask.png");
dialog.setButtons(new Button("OK"), new Button("Cancel"));
dialog.addButtonClickHandler(new ButtonClickHandler() {
public void onButtonClick(ButtonClickEvent event) {
dialog.hide();
}
});
dialog.draw();
ButtonClickHandler is an interface with onButtonClick as a only method.
This concept is called Anonymous Inner Classes
- An inner class without specifying a name
Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name
This is more often used in Writing Event Listeners such as Swing, GWT etc.
Anonymous classes are often used in graphical user interface (GUI) applications.