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

  • Home
  • SEARCH
  • 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 7829425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:45:23+00:00 2026-06-02T10:45:23+00:00

Here is my code: final Foo myFoo = new Foo(new Inner() { @Override callback(){

  • 0

Here is my code:

final Foo myFoo = new Foo(new Inner() {
    @Override
    callback(){
         myFoo.bar();
    }
});

(With actual function names)

final MyArrayAdapter aa = new MyArrayAdapter(new View.OnClickListener() {
    @Override
    onClick(){
         aa.notifyDataSetChanged();
    }
});

Java is giving me an error about how myFoo might not have been initialized. Is there any way to fix this? I can potentially set the callback to null when I construct the object and then change it afterwards, but I’d hope for there to be a cleaner way. Any ideas? (Also that wouldn’t work if Foo wasn’t written by me and didn’t expose an interface to change the callback later)

In case anyone is curious, in my specific scenario, Foo is an ArrayAdapter, and the bar is notifyDataSetChanged(). What is displayed by the adapter depends on the values of the items in the array, and those values change when they are clicked on. The callback is the clickListener.

  • 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-02T10:45:26+00:00Added an answer on June 2, 2026 at 10:45 am

    Short answer is that you definitely can’t do that in Java, but the compiler already told you that. You’re trying to create two objects at once with references to each other, it’s a chicken and egg problem. Bottom line, you have to create ONE of them first.

    A suggestion is a two step creation:

    ....
    final MyArrayAdapter aa = new MyArrayAdapter();
    aa.initializeButtonClickListener();
    ....
    

    and then add the ‘initialize’ method to the adapter.

    public void initializeButtonClickListener(){
        this.button.setOnClickListener(new View.OnClickListener() {
            @Override
            onClick(){
                 notifyDataSetChanged();
            }
        });
    }
    

    Because that construction is somewhat complicated (ie, more complicated than simply calling a constructor), I’d recommend then pulling those two lines in to a MyArrayAdapter factory method, and making the constructor private.

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

Sidebar

Related Questions

Here is my sample code : final String[] items = {এক, দুই, তিন}; builder
my question is that I have made multimap.Here is the partial code. if(binary_search(final.begin() ,
Here my code jQuery.fn.dataTableExt.oSort['num-asc'] = function(a,b) { var x = a.replace( /<.*?>/g, ); var
here is code that I have: agent = Mechanize.new page = agent.get 'http://google.com' page.save
Here is some code outlining a problem I've been wrestling with. The final problem
This is the code: package com.XXX; public final class Foo { private Foo() {
I'm working with some code where one object, foo, is creating another object, bar,
This is my code [...] final String[] top_spinner_items = new String[35]; for (int i
Ok the error is showing up somewhere in this here code if($error==false) { $query
Here sample code: require 'nokogiri' require 'open-uri' begin doc = Nokogiri::HTML(open(url)) rescue puts Fehler

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.