0

I have a doubt if i have function that returns the object that is hold in a class that already exists and do something like this:

    Class System{

    ...
    Vector<User> clients;
    ...
    }


    //In another class ...
    User c = getOwner(String username);
    c.add_value(balance);


   //Trasaction class
User owner;
   ....

public User getOwner() {
    return owner;
}

Does it change the values of the object that i returned and its hold in another class? or does it make a copy of that object?if so how can i make it to change the object and not make a copy of that object?

exceltior
  • 103
  • 2
  • 12

1 Answers1

0

First of all , I think you meant

User c = getOwner(username);

Now,

public User getOwner() {
    return owner;
}

In java, references to objects are passed by value. So, If you do

c.add_value(balance);

Yes, the object will be changed.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104