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 8783593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:51:31+00:00 2026-06-13T20:51:31+00:00

I’m aware of 3 different ways to do callback type functionality in Java, but

  • 0

I’m aware of 3 different ways to do callback type functionality in Java, but I don’t fully understand the pros/cons of each.

Java APIs are rich with methods similar in nature to this:

Button b = new Button();
b.setClickListener(<implements ClickListener>);

I’m using “button” as a generic example, not to refer to any specific API.

1.) One way to use it is to have your class implement the interface and pass it as the argument. To handle multiple “buttons” you’d have to use whatever information the caller provides to the callback to differentiate.

MyClass implements ClickListener
...
Button b = new Button();
b.setClickListener(this);
...
public void click(ButtonEvent e)
{
...

2.) Another way is to make an anonymous ClickListener on the spot and have its “click” method contain the code you want to execute. This has some added benefit because you can add new parameters to the callback by using local final variables.

for(int i=0 ; i<10 ; i++)
{
  final int finali = i;
  buttons[i] = new Button();
  buttons[i].setClickListener(new ClickListener()
  {
    public void click()
    {
      buttonClick(finali);
    }
  });
}

3.) The last way is possible if you’re writing the class calling the callback. You could just anonymously override the callback method you want to catch. The most obvious downside is not being able to switch callbacks during the life of the object.

for(int i=0 ; i<10 ; i++)
{
  final int finali = i;
  buttons[i] = new Button()
  {
    public void click()
    {
      buttonClick(finali);
    }
  }
}

Option 3 seems to be the most “simple” in many ways. It doesn’t require various interfaces to be made, and the callback function is always where the initialization is.

So my question is: What factors come into play when considering these design choices?

  • 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-06-13T20:51:33+00:00Added an answer on June 13, 2026 at 8:51 pm

    Option 1 is often convenient, but becomes a problem if you want to listen to multiple objects, since you have to explicitly check which one has triggered the callback.

    Option 2 avoids this problem, since each listener is separate; it is the most flexible of the three approaches.

    Option 3 is a bad idea, since you are essentially taking over the Button rather than listening for events (i.e. this isn’t really a callback at all); you may interfere with other code that is interested in the button, and the code will be harder to extend. Don’t follow the Dark Side ;-).

    And often you won’t be writing the class that calls the callback…

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

Sidebar

Related Questions

I've tracked down a weird MySQL problem to the two different ways I was
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I don't have much knowledge about the IPv6 protocol, so sorry if the question

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.