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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:15:18+00:00 2026-06-15T19:15:18+00:00

I have two classes: public class A{ int money; ArrayList<Item> boughtItems; . . public

  • 0

I have two classes:

public class A{
   int money;
   ArrayList<Item> boughtItems;
   .
   .
   public void addBoughtItem(Item item){
      .
      .
   }
   public void adjustMoney(int adjustAmount){
      money = money + adjustamount;
   }
}

and another class :

public class B{
  ArrayList<Item> itemsForSale;
  .
  .
  public Item sellMeItem(Item item){
    .
    .
    return itemsForSale.get(...);
  }
}

Now in the main, I start an instance of class A, and an instance of class B.
inside class B, whenever I sell them an item, I want to update the purchasedItems and adjust the money of the class A instance in realtime (that means, inside the sellMeItem function of class B.)
Therefore I think I need the A instance to be global.
How can I reach the A object that is in the main from inside the class B sellMeItem function? (and class B instance is also in the main)

  • 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-15T19:15:19+00:00Added an answer on June 15, 2026 at 7:15 pm

    Therefore I think I need the A instance to be global.

    Not really, you could simply pass a reference to your instance of A to B, for example via a constructor:

    class B {
    
        private A a;
    
        public B(A a) {
            this.a = a;
        }
    }
    

    Then you can access a in B. And in your main:

    A a = new A();
    B b = new B(a);
    

    However, the fact that the 2 classes are so intimately related probably means that the way you have split responsibilities among them is not optimal.

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

Sidebar

Related Questions

Suppose we have two classes: class Base { private: int x; public: void f();
I have two classes: public class MultilingualString { public int Id { get; set;
Let's say we have these two classes: public class Base { public static int
Say you have two classes, order and customer: public class Customer{ public int CustomerId
I have two classes: public class TestClass1 { public int TestInt { get; set;
I have two classes below: public class Module { public int Id { get;
Say i have the following two classes: public class User { public int ID
I have these two classes @DatabaseTable public class User { @DatabaseField(generatedId=true) private int _id;
I have two classes public class A { public int BaseA {get;set;} } public
I have two classes: public class Foo { public int FooId {get;set;} public virtual

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.