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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:06:06+00:00 2026-05-21T16:06:06+00:00

I was wondering (there has to be an easy solution) if there is a

  • 0

I was wondering (there has to be an easy solution) if there is a way to make a number of objects, and use them in other classes and objects from those classes, without having to update them (in Java). An example:

Class A, B and C make objects a b and c.
Then I have class D, which makes object d.
Now I want to use d in a, change it, use d in b, change it there and use d in c and still have all the changes from a and b.
I know it’s possible with using d as an argument in the functions, and returning d, but that is not useful in the program I’m making at the moment.

Any help?

Ok, My example in code:

public class A {
   D d = new D ();
   A(D dObject){
     this.d = dObject;
   }
   public void add () {
     DObject.add(5);
   }
}

public class B {
   D d = new D ();
   B(D dObject){
     this.d = dObject;
   }
   public void add () {
     DObject.add(5);
   }
}

public class C {
   D d = new D ();
   C(D dObject){
     this.d = dObject;
   }
   public void add () {
     DObject.add(5);
   }
}
public class D {
   public int x=0;
   public void add (int y){
      x +=y;
   }
}

// in main class:
D d = new D();
A a = new A(d);
B b = new B(d);
C c = new C(d);

a.add();
b.add();
c.add();

// d.x should give 15 now. 
  • 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-21T16:06:07+00:00Added an answer on May 21, 2026 at 4:06 pm

    I’m not sure if I understand right – the code you posted does exactly what you say (d.x is 15), if you correct the syntax errors.

    Here is a version that should work:

    public class A {
       D d;
       A(D dObject){
         this.d = dObject;
       }
       public void add () {
         d.add(5);
       }
    }
    
    public class B {
       D d;
       B(D dObject){
         this.d = dObject;
       }
       public void add () {
         d.add(5);
       }
    }
    
    public class C {
       D d;
       C(D dObject){
         this.d = dObject;
       }
       public void add () {
         d.add(5);
       }
    }
    public class D {
       public int x=0;
       public void add (int y){
          x +=y;
       }
    }
    
    // in main class:
    D d = new D();
    A a = new A(d);
    B b = new B(d);
    C c = new C(d);
    
    a.add();
    b.add();
    c.add();
    
    // d.x should give 15 now. 
    

    If you want d to stay the same instead of changing (e.g. d.x == 0 at the end), you can do one of these:

    • make clones of the parameter dObject in the constructor of A, B, C, so each has its own D object.
    • make D immutable and let its add method instead return a new D object. The add method of A, B, C would then have the implementation d = d.add(5);.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering if there is an easy way to add the functionality to duplicate
Wondering if anybody out there has any success in using the JDEdwards XMLInterop functionality.
was wondering if there are anyone else here that has signed up with the
I see in richfaces that there is a single slider, wondering if anyone has
Wondering if there's any not-too-hard way to edit non-form text in html 4. I
Just wondering if there is any way (in C) to get the contents of
Just wondering if there is a better way to write the following PL/SQL piece
I'm wondering if there's a way to do what I can do below with
Wondering if there is any Text to Speech software available as a plug in
I was wondering if there are any alternatives to Microsoft's SQL Server Management Studio?

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.