This might be a bit of a wide question but I want to understand if it's the correct way to do it. Assume I have a couple of classes all doing operations on an object. First class takes the object, does some operations on it; after this seconds class takes it and does it's own operations and so on.

Each of these classes should have a variable of type object? And each of these objects should have it's variables initialised using a setter?

Something like

private Object b;
public void set_obj(Object a){
        b = a;
  • 1
    Could you add a bit more context? Which kinds of operations are they doing? What kinds of objects? – amon Oct 31 '18 at 10:11
  • When you have multiple classes working on the same object / data, that it often a hint that your design could be improved. However, without some more details it's hard to say. – user949300 Oct 31 '18 at 18:02
  • The example code doesn't make sense to me. Try to post something that would compile and actually do something. – Tomas Zubiri Nov 22 '18 at 7:43

There's no "correct" way to handle this. That comes down to the needs of the project. We can comment on the complexity though, which will have an effect on how easy it is to manage the code going forward.

If the Object is stateless or a singleton, it'll be easy to manage. All references to it will predictably behave the same way. For example:

latestMath = someclass.getMathAfterAdd(1, 2)
// It doesn't matter which Math obj you have since it doesn't store state
// latestMath.add() will do the same as Math.add() with no side effects

If the Object has state the complexity increases since b.name = 'changed' will change a.name in the other objects that reference the same object. You can mitigate this increase somewhat by making the state changes transparent. How complexity increases:

updatedPerson = someclass.changePersonName('joe') 
// is updatedPerson.name == 'joe'? I'd assume so
// is person.name == 'joe'? hmmm
// is anotherclass.person.name == 'joe'? hmmm

Whether or not the increased complexity is worth it is situational.


A class doesn't perform any operations on their instances of objects. A class merely defines what the operations are, among other things.

The only way to pass the same object to two functions of different classes is if one class inherits from the other, and the Object is an instance of the inherited class.

The other way would be to weakly type the object upon variable definition and function definition. The downside to this is that you will only be able to perform operations where an Object is expected, perhaps do something like creating a copy.


Generally speaking, you wouldn't hold instances of objects unless you either didn't care what type the instance is or you must use object because of limitations, such as a library requiring object being passed.

Of course you could, but in order to do anything, you must cast it to a derived class and downcasting is generally frowned upon. Look for trends with these instances, and try to create a basic interface which these instances can extend. This way, rather than pass objects and downcast them, you can simply use its interface without caring how it is implemented.

Also, passing an instance from class to class sounds to me like chain of responsibility or decorator pattern to me. You might want consider using these for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.