Java "new" keyword in parameter
java, oop
Solution
Here, the `new` keyword is creating an anonymous class.
This is useful when you need a listener to perform some action, and you'd like to keep your code grouped together, and/or the class is "one-off", meaning it has no use elsewhere.
Here's a link to sun's tutorial on anonymous classes. All the normal rules of classes apply. You need to implement abstract methods, or all methods when creating an interface.
Scope is a little different as you can access variables declared in the class your anonymous class is nested within. However, you can't access local variables from an anonymous class unless those local variables are declared final. For instance:
Button button = new Button(shell, SWT.PUSH);
final String someString = "hello world!";
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// Handle the selection event
System.out.println(someString);
}
});
If `someString` were declared in more global scope, this would not be the case.
To your second question:
Yes. You are correct, that's what's happening in the snippet. Notice a new `PropertyChangeEvent` is being created every time? This is so listeners appearing earlier in the list don't modify the `PropertyChangeEvent` for items appearing later in the list.
Problem
I've been looking through a lot of OOP Design Patterns lately, and I've run into some strange things I've never seen before: ``` Button button = new Button(shell, SWT.PUSH); button.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { // Handle the selection event System.out.println("Called!"); } }); ``` Specfically, what does this do (eg. what does the "new" keyword do here)?: ``` button.addSelectionListener(new SelectionAdapter() { ``` Second question: ``` private void notifyListeners(Object object, String property, String oldValue, String newValue) { for (PropertyChangeListener name : listener) { name.propertyChange(new PropertyChangeEvent(this, "firstName", oldValue, newValue)); } } ``` This is a snippet from an observer design pattern. To my new understanding, the name.propertyChange(...) creates an object of PropertyChangeEvent and through Java's observer pattern implementation, automatically notifies the observers by sending this new object's information to the observers (or something very similar to this). Is this correct?