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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:47:51+00:00 2026-06-18T22:47:51+00:00

I have two classes as such: public class A{ ArrayList<Runnable> classBList = new ArrayList<Runnable>();

  • 0

I have two classes as such:

public class A{
    ArrayList<Runnable> classBList = new ArrayList<Runnable>();
    int x = 0;

    public A(){
        //This code here is in a loop so it gets called a variable number of times
        classBList.add(new B());
        new Thread(classBList.get(classBList.size())).start();
    }
}

public class B implements Runnable{
    public B(){

    }

    public void run(){
        //Does some things here. blah blah blah...
        x++;
    }
}

The problem is that I need to have the instance of class B change the variable x in class A, the class that created class B. However, I do not know how I would let class B know that it needs to change the value or if it can. Any suggestions on how to change it would be greatly appreciated. Thank you!

  • 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-18T22:47:52+00:00Added an answer on June 18, 2026 at 10:47 pm

    You need to give your B instance access to the A instance. There are a couple of ways to do that:

    1. Make B derive from A and make the data fields (or accessors for them) protected in A. I would tend to shy away from this one.

    2. Make B accept an A instance in its constructor.

    3. Make B accept an instance of a class that implements some interface in its constructor, and have A implement that interface.

    Which you choose is up to you. I’ve given them in roughly decreasing order of coupling, where the more loosely-coupled, the better (usually).

    That third option in code:

    public TheInterface {
        void changeState();
    }
    
    public class A implements TheInterface {
        ArrayList<Runnable> classBList = new ArrayList<Runnable>();
        int x = 0;
    
        public A(){
            //This code here is in a loop so it gets called a variable number of times
            classBList.add(new B(this)); // <=== Passing in `this` so `B` instance has access to it
            new Thread(classBList.get(classBList.size())).start();
        }
    
        // Implement the interface
        public void changeState() {
            // ...the state change here, for instance:
            x++;
        }
    }
    
    public class B implements Runnable{
        private TheInterface thing;
    
        public B(TheInterface theThing){
            thing = theThing;
        }
    
        public void run(){
            // Change the thing's state
            thing.changeState();
        }
    }
    

    Now, both A and B are coupled to TheInterface, but only A is coupled to B; B is not coupled to A.

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

Sidebar

Related Questions

I have two classes defined as such: public class Questionnaire { public int QuestionnaireID
I have two classes, shown below: [Serializable] [XmlInclude(typeof(SomeDerived))] public class SomeBase { public string
I have these two classes public class BuyEstimateResult { public string SubTitle { get;
I have a relationship between two base classes: public abstract class RecruiterBase<T> { //
Assume we have such code: public class Observer { public event EventHandler X =
I have two classes: class MyClassInfo { public: void AddMethod(std::string name, void* pointer); //
I have created two viewController classes such that one is superclass of another.i have
I have two classes - MyBaseClass and BaseClassContainer - that are declared like such:
I have two classes (MVC view model) which inherits from one abstract base class.
I have two classes Foo and Bar that Bar extends Foo as below: class

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.