Java "new" keyword in parameter

I have been browsing through a lot of OOP design patterns lately, and I came across some strange things that I have 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!"); } }); 

In particular, what does it do (for example, 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 a PropertyChangeEvent object and, through the implementation of the Java observer template, automatically notifies observers, sending this new information about the object to observers (or something very similar to this). Is it correct?

+7
java oop
source share
3 answers

Here, the new keyword creates an anonymous class.

This is useful when you need a listener to perform some actions, and you want your code to group together and / or the class is “one-time”, which means that it is not used elsewhere.

Here's a link to the sun tutorial on anonymous classes. All normal class rules apply. You need to implement abstract methods or all methods when creating an interface.

Scaling is a bit different, since you can access variables declared in a class, your anonymous class is nested inside. However, you cannot access local variables from an anonymous class, unless these local variables are declared final. For example:

 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 a more global scope, it is not.

To your second question:

Yes. You are right that what is happening in the fragment. Note that a new PropertyChangeEvent is created each time? This means that listeners that appear earlier in the list do not change the PropertyChangeEvent for elements that appear later in the list.

+23
source share

First of all, this is Java-specific syntax: there is no comparable syntax in C # where you must either create an anonymous class that extends object , or create an instance of a named class.

In Java, this syntax allows you to create an anonymous subclass of SelectionAdapter , overriding any methods of your choice. This is equivalent to creating a named class that extends the SelectionAdapter , overriding any methods, as in curly braces, after calling the SelectionAdapter() constructor, and then using the name of this derived class in the addSelectionListener call. The only difference is that such a derived class will have a name, and the anonymous class from your example does not have a name * available to programmers.

* Internally anonymous classes have names: you can see them if you look at the list of class files generated by the Java compiler. Files with dollar signs and numbers in their names correspond to anonymous classes.

+4
source share

The new keyword creates an anonymous object.

This is useful if you want to create an instance that is used only once.

0
source share

All Articles