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 methodincrement()
, that increases an int value. - We have also created a class,
MyIncrement
that consists of a methodincrement()
and astatic
methodf(MyIncrement mi)
that gets aMyIncrement
object and calls itsincrement()
method. - Another class is
Callee2
that extendsMyIncrement
and consists of a method,incr()
and a private classClosure
that implements theIncrementable
and overrides itsincrement()
method, where it calls itsincr()
method. It also has a methodgetCallBackReference()
that returns a newClosure
instance. - We have another class,
Caller
that consists of anIncrementable
, it has a constructor using theIncrementable
and a methodgo()
that calls theincrement()
method ofIncrementable
. - We create a new instance of
Callee1
andCallee2
. - When calling
f(MyIncrement mi)
method ofMyIncrement
, usingCallee1
object as parameter it callsincrement()
method ofMyIncreament
. - When creating a new instance of
Caller
, using theCallee1
object in its constructor, it returns theCallee1
object, so when itsgo()
method is called , theCallee1
object’s private int value is incremented and returned. - When creating a new instance of
Caller
using in its constructor a newClosure
that theCallee2
object’sgetBackREference()
returns, a newClosure
object is returned. So when callinggo()
method, theClosure
object that is an inner class inCallee2
increments the int i ofCallee2
.
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.