Results 1 to 7 of 7
Thread: Don't pass by reference
- 11-14-2009, 07:56 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 1
- Rep Power
- 0
Don't pass by reference
Hello
I try to make a dummy of an instance of a self made class Calender.
Afterwards I make a change into the instance of the class Calender.Java Code:Dummy = Calender.getCalender();
The randomMove() function updates the Calender with an algorithm.Java Code:Calender.RandomMove();
Afterwards I want to compare the changes made by the RandomMove() function. So I compare it with Dummy. Problem: the Dummy variable is equal to Calender because Calender.RandomMove() also updates the Dummy. Obviously something due to a pass by ref. But how can Solve this problem. I also tried a copy constructor.
Thanks in advance
Lyven
- 11-15-2009, 05:18 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
In Java everything is passed by value: primitive values are passed by value, reference values are passed by value.
It would be a very good idea to actually post your code. (And also to use standard Java naming conventions: start class names with an upper case letter, and method and variables with a lower case letter.)But how can Solve this problem. I also tried a copy constructor.
A copy constructor sounds like it may be the way to go. But that's only a guess without seeing your code. Note that what you did post is ot a constructor at all.
Java Code:Dummy = Calender.getCalender();
"the Dummy variable is equal to Calender", if true, suggests that getCalender() returns "this". And that is that is not a very good idea if you expect methods called on Calender not to be reflected in the state of Dummy.
- 11-15-2009, 05:14 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 23
- Rep Power
- 0
You're right of course, but I think people get confused by the semantics and not the concept. Yes it's pass by value, but the value is a reference. I honestly think that many people incorrectly say "pass by reference" but they probably mean "pass a reference".
So what I am saying is that many people have the mechanics, which really aren't that hard, right but the semantics (wording) wrong.
- 11-16-2009, 08:49 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 11-16-2009, 03:05 PM #5
Member
- Join Date
- Nov 2009
- Posts
- 23
- Rep Power
- 0
Fine. I see your point. I'm sure we both understand full well how pass by value works. If I didn't, I'd have a big problem. but there is a lack of consensus on the semantics. So we end up splitting hairs on the exact meaning of things like variable, and reference, and value. One person says "copy of reference", another person says a copy of the reference's value, someone else says the reference is the value, someone else says the reference is a copy of the value of the reference variable, and we end up dicking around with words when we both know full well how it works. I can change my wording no problem, if it helps me to make a point.
So my main point still stands. I still say most confusion is really about the semantics of phrases like "pass by reference", and not misunderstandings about how object references as arguments and parameters really works. And I expect that there will be disagreement about that claim too.
Anyways, yeah, to solve the problem we need to see the problem code. We agree on that, I was just commenting on the age-old argument about object references, which was the title of the thread.Last edited by fgm1; 11-16-2009 at 03:09 PM.
- 11-16-2009, 05:39 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
The "semantics" are important. You can visualise it however you wish, but if you visualise it wrongly, you will, eventually, have problems. Don't go telling others your view of it, tell them the proper semantics or you will set them off on the wrong path.
- 11-16-2009, 06:06 PM #7
Member
- Join Date
- Nov 2009
- Posts
- 23
- Rep Power
- 0
You missed my point completely. I wasn't trying to tell anyone how it works, sounds like you were telling me. I can see you have a problem with what I say, but I'm not sure how to fix that, or even if it's worth trying.
One last time, then I'll leave it to the "experts". All I'm saying is, a lot of the people who use the phrase "pass by reference" use it incorrectly. They understand how object references in java work, but they use the wrong words to describe the process. In which case simply saying "There is no such thing as pass by reference in java", while correct, clarifies nothing. Can't make it any clearer than that. If you disagree well then we shall have to disagree.
Similar Threads
-
Object and reference
By katie in forum New To JavaReplies: 2Last Post: 10-19-2009, 03:45 PM -
Pass-by-reference
By popjava in forum New To JavaReplies: 1Last Post: 10-19-2009, 03:45 PM -
creating a reference to a value in a map
By kellykellykelly in forum Advanced JavaReplies: 1Last Post: 02-28-2009, 07:59 AM -
removing reference
By ajith_raj in forum Advanced JavaReplies: 4Last Post: 02-12-2009, 11:46 AM -
Getting the Object Reference Name
By Deathmonger in forum New To JavaReplies: 2Last Post: 03-12-2008, 02:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks