Gerstmann
Gerstmann

Reputation: 5518

How to cope from not being able to pass by reference?

I'm finding it incredibly difficult to comprehend how different objects should communicate and exchange information.

Coming from the C/C++ world, I'm used to pass objects by reference when I need to give an object to a class/function for processing.

I'm certain that there's a we'll known pattern for achieving clean and maintainable way for object communication. I just need to find out what it is.

EDIT: Example

ObjectThatNeedsProcessing obj;
WizardDialog dialog = new WizardDialog;
dialog.addObjectToBeProcessed(obj);
dialog.show();

//When the dialog is finished obj would be changed.

Best regards

Upvotes: 1

Views: 148

Answers (2)

Dave Richardson
Dave Richardson

Reputation: 5005

I'm no C++ programmer but in C you pass everything by value (even pointers are passed by value, they just point to something) - and it's the same in Java.

And for the next person who marks this as negative - please explain why so that everyone else understands.

Upvotes: 0

blessanm86
blessanm86

Reputation: 31789

When you pass an object to a method in java, another reference is made to the existing object. So you have 2 references to the same object.

enter image description here

Upvotes: 1

Related Questions