Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Functional development

843793Dec 16 2003 — edited Dec 20 2003
Sometimes it is pretty handy to pass through functions to other functions (as is done in functional languages). Such a feature should also exist in Java. These functions could heavily simplify eventhandlers and other listeners, eventually even thread development can be simplified like this. The following illustrates what I mean:

Old non-functional implementation
class SomeFrame extends Frame {
    Button button = new Button(...);
    SomeFrame {
        ...
        button.addActionListener(new ActionListener(this) {
            public void actionPerformed(ActionEvent e) {
                buttonClicked(e)
            }
        });
        ...
    }

    public void buttonClicked(ActionEvent e) {...}    
}
Functional implementation, much simpler:
class SomeFrame extends Frame {
    Button button = new Button(...);
    SomeFrame {
        ...
        button.addActionEvent(function this.buttonClicked);
        ...
    }

    public void buttonClicked(ActionEvent e) {...}    
}
// Somewhere in Button should be:
...
    void addActionEvent(void function(ActionEvent), func) {...}
...
It should then also be possible to make collections out of functions.

Suggestions please...
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 17 2004
Added on Dec 16 2003
14 comments
211 views