0

I am using this post as my guidance.

I want to know if I understand the following concept of pass by value in java clearly.

I have a java object and I pass it by value (so the address of the object, I guess in this case) to a function that calls a class constructor. The address of my main object is 'copied' to the constructor object. Now this constructed object and my main object are at the same address? or are pointing to the same address?

void main(){

 Result result = new Result(); //empty object

 Timer timer = new Timer();
 timer.schedule(new UpdateResultTask(result);

 result.getValue(); has some valid values. Is not empty anymore.

}

public class UpdateResultTask extends TimerTask {

    private Result result;

    //Constructor 
    public UpdateResultTask(Result result) {
        this.result = result;
     }

     @Override
     public void run() {
     // the local result object gets filled here
     //where response is some http response
     result = response.getEntity(Result.class);
    }
}

Since the change inside the local function is reflected outside in the main function, i assume that its because that at the time of construction, both the new and the old (main) object have the same address?

Do I understand it correctly? In java, pass by value for objects is just passing the copy of the objects address?

Mohit Tyagi
  • 2,788
  • 4
  • 17
  • 29
rehas
  • 176
  • 3
  • 10
  • 2
    Could you update your example so it compiles and runs? And tell us exactly what you're seeing, what you expect to see, and (if relevant) what about that you don't understand... – T.J. Crowder Oct 02 '17 at 13:50
  • *"...so the address of the object, i guess in this case..."* Not literally (well, depending on one's interpretation of "address"), but in effect, yeah. – T.J. Crowder Oct 02 '17 at 13:50
  • 2
    With respect, the question is a bit confused. Certainly the assignment in `run` has no effect whatsoever on the `Result` object created in `main`. – T.J. Crowder Oct 02 '17 at 13:52
  • *"Now this constructed object and my main object are at the same address?"* - Would your bag become an apple when you put one into it? No, obviously not, it would just would contain one. So now why do you think `UpdateResultTask` would be on the same memory space like the `Result` object? `UpdateResultTask` ___contains___ a `Result`, but isn't one. – Tom Oct 02 '17 at 13:52
  • If your comments are to be believed, in `run` you first update the values in the outside object passed to the constructor and after that replace that reference with a new one from response. Which might or might not be the point that baffles you, I don't quite understand your questions surrounding the code – Deltharis Oct 02 '17 at 13:55
  • @T.J.Crowder Your answer seems to be as confusing as my question. when you say, "Not literally, but in effect yeah" which one is it than? Also, the assignment in run is reflected in main. That is why i thought they must be at the same address else i don#t understand why it is happening – rehas Oct 05 '17 at 09:39
  • @Deltharis The point that you mentioned is exactly the one that baffles me. i.e, Why are the changes in run reflected in the main? – rehas Oct 05 '17 at 09:41
  • @rehas think about the flow of your program. You pass a reference to Result as parameter for Task - at this point both main method and task object have reference pointing at the same Result. Than `run()` starts, and you edit some values in that object. Finally you replace reference in the task to point at another object (but that doesn't change the fact that you already changed the first one). – Deltharis Oct 05 '17 at 10:00

0 Answers0