• Post Reply Bookmark Topic Watch Topic
  • New Topic

For loop problem  RSS feed

 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
I really don't know why this stupid thing is happening. I sat for two hours trying to understand this bit of code that I wrote. Its behaving strange.


You see whenever I print the value of originalLocation I get a different value every time for the same phase(that is same for loop). I assign a particular value to originallocation before the loop starts but while debugging I notice that somehow the value of originalLocation is changin. Very Strange :roll:

[ EJFH: Try to fix formatting ]
[ May 10, 2005: Message edited by: Ernest Friedman-Hill ]
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not strange at all, if you understand that Java variables are references to objects, and that assigning a variable copies the reference, not the object. Take a few minutes and read this and this and you will achieve enlightenment!
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see a variable named location but no declaration for it. I assume this is a class variable? Can you post the declaration and intialization for this variable. At least for me, it will help clear things up a little.

Layne
[ May 10, 2005: Message edited by: Layne Lund ]
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
I found out why this is happening.


In the above line a reference of the originalLocation object was being passed to location object(which I overlooked). And so when the value of location changed the value of originalLocation also changed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!