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:  

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.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

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
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button