class

Use inner class for callbacks

With this example we are going to demonstrate how to use inner class for callbacks to a class. The example is described in short:

  • We have created an interface, Incrementable and a class to implement it, Callee1. It has a method increment(), that increases an int value. 
  • We have also created a class, MyIncrement that consists of a method increment() and a static method f(MyIncrement mi) that gets a MyIncrement object and calls its increment() method.
  • Another class is Callee2 that extends MyIncrement and consists of a method, incr() and a private class Closure that implements the Incrementable and overrides its increment() method, where it calls its incr() method. It also has a method getCallBackReference() that returns a new Closure instance.
  • We have another class, Caller that consists of an Incrementable, it has a constructor using the Incrementable and a method go() that calls the increment() method of Incrementable.
  • We create a new instance of Callee1 and Callee2.
  • When calling f(MyIncrement mi) method of MyIncrement, using Callee1 object as parameter it calls increment() method of MyIncreament.
  • When creating a new instance of Caller, using the Callee1 object in its constructor, it returns the Callee1 object, so when its go() method is called , the Callee1 object’s private int value is incremented and returned.
  • When creating a new instance of Caller using in its constructor a new Closure that the Callee2 object’s getBackREference() returns, a new Closure object is returned. So when calling go() method, the Closure object that is an inner class in Callee2 increments the int i of Callee2.

Let’s take a look at the code snippet that follows:  

package com.javacodegeeks.snippets.core;

interface Incrementable {

    void increment();
}

// Very simple to just implement the interface:
class Callee1 implements Incrementable {

    private int i = 0;

    @Override
    public void increment() {

  i++;

  System.out.println(i);
    }
}

class MyIncrement {

    void increment() {

  System.out.println("Other operation");
    }

    static void f(MyIncrement mi) {

  mi.increment();
    }
}

// If your class must implement increment() in
// some other way, you must use an inner class:
class Callee2 extends MyIncrement {

    private int i = 0;

    private void incr() {

  i++;

  System.out.println(i);
    }

    private class Closure implements Incrementable {


  @Override

  public void increment() {


incr();

  }
    }

    Incrementable getCallbackReference() {

  return new Closure();
    }
}

class Caller {

    private Incrementable callbackReference;

    Caller(Incrementable cbh) {

  callbackReference = cbh;
    }

    void go() {

  callbackReference.increment();
    }
}

public class Callbacks {

    public static void main(String[] args) {

  Callee1 c1 = new Callee1();

  Callee2 c2 = new Callee2();

  MyIncrement.f(c2);

  Caller caller1 = new Caller(c1);

  Caller caller2 = new Caller(c2.getCallbackReference());

  caller1.go();

  caller1.go();

  caller2.go();

  caller2.go();
    }
}

Output:

Other operation
1
2
1
2

  
This was an example of how to use inner class for callbacks to a class in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button