Смарт контракты

Published 15 October 2018

Callback methods in Java

What is a callback method in Java. What is it used for

What is a callback method in Java. What is it used for.

Callback method performs the transfer of executable code as one of the parameters to another method; this executable code will be executed inside the method it was passed to.

A callback method can be used to compute the hash value from the string to bypass any object storage using some action to each object. When a method is called, it is passed a pointer to the callback method.

Interfaces are used to create a callback method in Java.

Example of a callback method

Example of a callback method

import java.util.ArrayList;

import java.util.Random;


public class CallBackWork {

static class Cal implements Callback {

private ArrayList<Integer> stat = new ArrayList<>();

public ArrayList<Integer> getStat(){

        return stat;


  public void callMeBack(int status){

  synchronized (stat) {





static interface Callback {

    void callMeBack(int status);


static class Worg extends Thread {

private Callback cv;

public Worg(Callback cv) {

        this.cv = cv;}

  int Favor() {

        return new Random().nextInt();


    public void run() {

        int sn = Favor();




public static void main(String[] args) {

Cal calk = new Cal();

  for (int i = 0; i < 8; i++){

        new Worg(calk).start();





Pros and cons (callback)

Pros and cons (callback)

  • unlimited variations of the called method without changing the underlying code;
  • the use of the called method as a subroutine to track operations or change parameters for the next called method;
  • degradation of source code readability.
Votes 0, average rating 0