Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3311020
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:49:39+00:00 2026-05-17T21:49:39+00:00

I came across this question about callbacks in Java. Hers is the running code

  • 0

I came across this question about callbacks in Java. Hers is the running code and original answer here.

  1. But I didn’t understand how is it useful for callback?
  2. Can you explain the concept of callback to a Java programmer?

Code:

public class Main {

    public interface Visitor {
        int DoJob(int a, int b);
    }


    public static void main(String[] args) {
        Visitor adder = new Visitor(){
            public int DoJob(int a, int b) {
                return a + b;
            }
        };

        Visitor multiplier = new Visitor(){
            public int DoJob(int a, int b) {
                return a*b;
            }
        };

        System.out.println(adder.DoJob(10, 20));
        System.out.println(multiplier.DoJob(10, 20));

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:49:40+00:00Added an answer on May 17, 2026 at 9:49 pm

    I wrote a small blog post on this sometime back : http://madhurtanwani.blogspot.com/2010/09/callbacks-in-java.html. Hope it helps!

    Before I try explaining the above code post, I must say, its not the most intuitive or good use of call backs. The example I’ve used in my post is of Collections.sort() which clearly brings out the callback part.

    Neverthelss, for the code posted above, think of like this :

    1. The main() function actually is a number (read Data) stream parsing algorithm, which will parse a pair of numbers (data sets) and then operate on them.
    2. Now, considering separation of concerns, the main() function should neither know or understand what operations can be performed on the datasets. In fact it should not even care of the data types of the data sets.
    3. However, since these data sets are specific to your domain, the main function should ideally delegate the processing of the data to your domain specific classes.
    4. For it to do so, it must enforce a contract with the domain specific caller (the caller for main) and say – look, I’ll call the doJob method on a Visitor interface implementations, whenever I receive a pair of data sets. What the caller must do is implement the Visitor interface and implement the domain specific logic to process the datasets.

    The part of delegating processing from the caller, back to the callee is called a callback implemented using interface (contract specification) in Java.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just came across this question about initializing local variables. Many of the answers
I came across this answered question , but I can't seem to compile the
A bit of a neophyte haskell question, but I came across this example in
I came across this question about memory management of dictionaries, which mentions the intern
I came across this question on an interview questions thread. Here is the question:
While researching the issue of JSON vs XML , I came across this question
I came across this snippet of code on MSDN: entityBuilder.Metadata = @res://*/AdventureWorksModel.csdl| res://*/AdventureWorksModel.ssdl| res://*/AdventureWorksModel.msl;
I came across this question on transcender: What should you apply to a field
I came across this question recently - What is function try block handler? Also,
I was looking around for jQuery grid recommendations and came across this question/answers: https://stackoverflow.com/questions/159025/jquery-grid-recommendations

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.