Results 1 to 7 of 7
  1. #1
    Lyven is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Default Don't pass by reference

    Hello

    I try to make a dummy of an instance of a self made class Calender.
    Java Code:
    Dummy = Calender.getCalender();
    Afterwards I make a change into the instance of the class Calender.

    Java Code:
    Calender.RandomMove();
    The randomMove() function updates the Calender with an algorithm.

    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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Quote Originally Posted by Lyven View Post
    Obviously something due to a pass by ref.
    In Java everything is passed by value: primitive values are passed by value, reference values are passed by value.

    But how can Solve this problem. I also tried a copy constructor.
    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.)

    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.

  3. #3
    fgm1 is offline Member
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    In Java everything is passed by value: primitive values are passed by value, reference values are passed by value.
    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.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by fgm1 View Post
    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.
    No, the "value" is a copy of the reference's value. The "value" is not the "reference".

    It would very much help to see the "problematic" code.

  5. #5
    fgm1 is offline Member
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    No, the "value" is a copy of the reference's value. The "value" is not the "reference".

    It would very much help to see the "problematic" code.
    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 04:09 PM.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  7. #7
    fgm1 is offline Member
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    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.
    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

  1. Object and reference
    By katie in forum New To Java
    Replies: 2
    Last Post: 10-19-2009, 04:45 PM
  2. Pass-by-reference
    By popjava in forum New To Java
    Replies: 1
    Last Post: 10-19-2009, 04:45 PM
  3. creating a reference to a value in a map
    By kellykellykelly in forum Advanced Java
    Replies: 1
    Last Post: 02-28-2009, 08:59 AM
  4. removing reference
    By ajith_raj in forum Advanced Java
    Replies: 4
    Last Post: 02-12-2009, 12:46 PM
  5. Getting the Object Reference Name
    By Deathmonger in forum New To Java
    Replies: 2
    Last Post: 03-12-2008, 03:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •