Solved

Are two variables referring to the same object?

Posted on 2014-07-25
5
115 Views
Last Modified: 2014-07-25
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
Comment
Question by:monoceros
  • 2
  • 2
5 Comments
 
LVL 32

Expert Comment

by:ste5an
ID: 40219226
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
 
LVL 1

Author Comment

by:monoceros
ID: 40219252
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 40219270
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
 
LVL 1

Author Closing Comment

by:monoceros
ID: 40219352
How could I manage this if toString has been overridden?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40219446
You can't really
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now