JDK-6500704 : Add language support for converting methods to interfaces
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 5.0
  • Priority: P5
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-12-05
  • Updated: 2011-10-31
  • Resolved: 2011-10-31
Related Reports
Duplicate :  
Relates :  
Relates :  
Relates :  
A method with the same signature (except the name) as the sole method in an interface could be converted, either automatically (coercion) or explicitly, to that interface.

This could be considered syntactic sugar for inner classes that simply wrap a specific function, and it could also be implemented like that (but more efficient implementations may be possible).

The goal would be to provide a type-safe and simple to use mechanism for event handling similar to "procedure of object" pointers in Object Pascal / Delphi or (to a lesser degree) signals and slots in Qt or the boost C++ libraries.

This solution will work with many existing interfaces and event source implementations.  Only the code for binding listeners will be simplified, which is both the most commonly used and (in my option) the most cumbersome part today.

Like (anonymous) inner classes, it does not publicly expose the interface being implemented.


-- PositionChangeHandler.java:

public interface PositionChangeHandler {
   public void handlePositionChange(Event e, int x, int y);

-- EventSender.java:

public class EventSender {
   public void addPositionChangeListener(PositionChangeHandler handler) {

-- MyEventReceiver.java:

public class MyEventReceiver {
   private EventSender sender1;
   private EventSender sender2;

   private void posChanged1(Event e, int x, int y) {

   private void posChanged2(Event e, int x, int y) {

   private void connectEvents() {
      /* The following has the same effect as:
      sender1.addPostionChangeListener(new PositionChangeHandler() {
         public void handlePositionChange(Event e, int x, int y) {
            posChanged1(e, x, y);
      sender2.addPostionChangeListener(new PositionChangeHandler() {
         public void handlePositionChange(Event e, int x, int y) {
            posChanged2(e, x, y);


Alternative solution:

Maybe the conversion from method to interface should be made explicit instead of automatic. It could also be useful to have a way to bind several methods for interfaces defining more than one than one function interface.

An example (incomplete, but you get the point):

interface MyInterface {
  void method1(int arg);
  void method2();

class MyClass {
  private void handler1(int arg) {}
  private void handler2() {}
  private void attachEvents {
    // a better syntax can probably be found
    anEventSource.addListener(MyInterface(method1 = handler1, method2 = handler2));

Use anonymous inner classes (cumbersome) or reflection (no compile-time type checking).

EVALUATION Proposals for new features in the Java programming language are no longer being accepted or maintained in the bug database at http://bugs.sun.com/. This also applies to proposals for new features in the Java Virtual Machine (that is, in the abstract JVM, as opposed to a JVM implementation like HotSpot). All proposals for new features should be made through the "JDK Enhancement - Proposal & Roadmap Process" described at http://openjdk.java.net/jeps/1. This specific request to change the Java programming language is related to the work of OpenJDK Project Lambda. It will be considered further by that project, not here. The bug database continues to accept and maintain submissions about technical errors in the design and specification of the Java programming language and the Java Virtual Machine.