Women in Technology

Hear us Roar

  10 Reasons We Need Java 3.0
Subject:   Steal Objective-C's selectors, use inner classes far less.
Date:   2002-08-06 06:34:33
From:   ewatkeys
One of the things I've always found painful with Swing is the need to define tons of inner classes to hook user interface elements to a method in a specific object. In a non-object oriented world, it would make sense to pass in a function pointer, and that's what AbstractAction and friends amount to. I believe that the C++ FAQ book calls such one-method objects functoids.

Anyway, Objective-C has a wonderful feature called a selector. It allows a programmer to specify a specific method on a specific object. So instead of this horrific code in Java 1.2:

button = new JButton(new AbstractAction("Throb") { public void performAction(ActionEvent e) { throb(); } });

One would be able to write the following code:

button = new Button("Throb", selector(throb));

While inner classes may have some other benefits, I have rarely used them outside of Swing code, and almost always in a way that could have more elegantly been replaced by a selector.