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:
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 | 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.