There’s no problem there, it’s happening what the code says to do, if that’s not what you want to do.
You take an object referenced in a variable called Parametros
and it says that you want that reference to be placed in a variable called params
, so the same object happens to have two references. I imagine you understand what reference is because you put in the title.
So every time you change the referenced object or by params
or by Parametros
is changing the same object. And of course every time you access this object you will see the change, no matter who has changed.
If you don’t want this, don’t do it like this. Then you need to see what you want.
It may be that you wish to have two different objects. Almost always this is a mistake and may even be the fault of wrong desire. Looking up, without seeing larger context, it doesn’t look like you want to have two objects, but you want behavior that doesn’t make sense.
If you really want two objects you need to think about the copy strategy, even more so that there are levels like this. How much should you copy the object? Everything? Want to choose just a few parts to do it? It starts to get complicated. And worse, if you don’t think about it and think it’s easy you can make a mistake without understanding why.
If you really want this you have one Soen asks how to clone the object in the right way. Cloning the object makes it independent, but it still feels like an error.
Maybe you want to create a prototype and you’re creating an object, you need to define it better.
I understood, I was thinking that the way I did, the object was "copied". Thank you very much!!!
– Matheus Souza
It is quite likely that the use of a class would be more appropriate than cloning the object, but there is no way to state without knowing how it is being used.
– Andre