Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 141
  • Last Modified:

Are two variables referring to the same object?

I am debugging some Groovy code for a website (that I did not write) and have hit an issue where I create an object A in controller in one part of the flow and set a variable within it (read it back and its correct).

I then pick up what I had understood to the the same object in a different controller. But the variable is no longer set.

Either my assumption that the object A in the first controller is the same object A as picked up the the second controller is wrong or something has modified the value en-route.

So, what might be a very basic question (and I have have a horrible feeling that it points to some fundamental misunderstanding on my part of how Groovy/Java works - so please be gentle) :
How can I tell if the object A in controller 1 is the same as the object A in controller 2 (by the same I mean point to the same object, not that they are equivalent).
0
monoceros
Asked:
monoceros
  • 2
  • 2
1 Solution
 
ste5anSenior DeveloperCommented:
Java: == tests for whether two variables point to the same memory location (identity). Per default this does also equals(). But equals() can and often is overwritten (also is hasCode()) to test for value equivalence only.

Groovy: == tests for value equivalence. Use .is for testing for identity.

One question is: How is the object passed from controller 1 to controller 2? It sounds like it is only a shallow copy.
0
 
monocerosAuthor Commented:
Ste5an, thanks - reading your answer made me realise that I was not completely clear in my question: I do not have access to both variables at the same place & time so I cannot test for identity with .is - so what I really need is something that can be generated to a log file that allows me to see that both variables refer to the same object.

To answer your question: I am still trying to figure it out! There is much use of re-entrant code driven by state and its quite hard to map out all the possible paths (I currently have the horrible feeling that the objects are actually re-created in error later in the flow - which would explain what I am seeing).
0
 
CEHJCommented:
If the object doesn't override toString, you can simply print the reference to the log file and later ascertain that the references are the same (or not)
0
 
monocerosAuthor Commented:
How could I manage this if toString has been overridden?
0
 
CEHJCommented:
You can't really
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now