1

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

Gerstmann
  • 5,368
  • 6
  • 37
  • 57

2 Answers2

1

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

blessanm86
  • 31,439
  • 14
  • 68
  • 79
0

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.

Dave Richardson
  • 4,880
  • 7
  • 32
  • 47
  • (I voted up) yes C passes everything by value, but you have the additional ability to take the address of variables, which you cannot do in Java. For example, if you want to let a function modify a pointer you have in your scope, you could take its address (to get a pointer to a pointer), pass it to the function, and it can modify the original pointer through that pointer, which you cannot do in Java. Also, you can take the address of primitives (e.g. get an int pointer) and pass it to a function for it to modify, which you cannot do in Java short of using an array or mutable container class. – newacct Oct 21 '11 at 23:12
  • Thanks for the upvote :) I guess you could argue that pointers to pointers are a feature of C/C++ for which there is no direct java equivalent. That said, I haven't needed such a feature with java - it's cleaner to write code with simple interfaces. – Dave Richardson Oct 21 '11 at 23:46