Passing by reference, more confusion...

Discussion in 'Java' started by harry, May 9, 2004.

  1. harry

    harry Guest

    I have an object that's passed in to a function as a parameter i.e public
    boolean getProjectTitle(ProjectHeader_DTO obj) {...}

    If I then call a method on this object inside the function i.e
    obj.setTitle("test") then using obj.getTitle() from outside the function it
    displays the property set correctly!

    But doing something like this inside the function doesn't -

    ProjectHeader_DTO x = new ProjectHeader_DTO();
    x.setProjectTitle("test");
    obj=x;

    As long as I explictly set the properties it works but why not when
    assigning another object?

    Tried using the clone() method aswell & inside the function its works fine
    until after the return!

    I do not want the object to be return'ed from the function!

    Any ideas?

    thanks

    harry
    harry, May 9, 2004
    #1
    1. Advertising

  2. harry wrote:
    > I have an object that's passed in to a function as a parameter i.e public
    > boolean getProjectTitle(ProjectHeader_DTO obj) {...}
    >
    > If I then call a method on this object inside the function i.e
    > obj.setTitle("test") then using obj.getTitle() from outside the function it
    > displays the property set correctly!
    >
    > But doing something like this inside the function doesn't -
    >
    > ProjectHeader_DTO x = new ProjectHeader_DTO();
    > x.setProjectTitle("test");
    > obj=x;
    >
    > As long as I explictly set the properties it works but why not when
    > assigning another object?


    When you call a function, the parameters are copied and only a copy of
    the varible is used inside the function ( this is why passing int x does
    not change x outside the function )
    When you pass an object, not the object itself is passed but a pointer
    to the memory of the object, the pointer is copied, but still shows to
    the same memory. So obj.setTitle("test") inside the function changes the
    title from the object obj points to ( and this is the same mermory, obj
    outside the function points to ). When you create a new object x, x
    points to some other memory. The statement obj=x says: make obj point to
    where x points to ( to the newly allocated memory ). Assuming x was
    created inside the function, x gets destroyed, as soon as you leave the
    function. After the function, obj still points to the old obj-memory,
    because the obj you assigned to x is only a copy of obj

    think of it as houses and addresses: You have a green house ( you need
    memory (space) for your house ) and you have a piece of paper with its
    address: 5th avenue. If you copy or change the address on the paper, you
    don't change anything about your house.
    consider following pseudo-code:

    void clean_house ( House green ) // passing copy of your paper with address
    {
    green.make_house_nice_and_clean(); // address points to the green house
    }
    -> green house is clean now!

    void clean_house ( House green)
    {
    House red = new House; // building a new house somewhere else
    green = red; // Address now indicates the red house
    green.make_house_nice_and_clean(); // cleaning the red house!!!!

    }
    -> green house is still dirty

    If you tell somebody to clean your house, it is impossible, to "give"
    him the house, you rather leave the address - same with java: coping and
    moving around all the mermory for the house is much less efficent than
    passing the address to this mermory.

    Hope this was helpful for you, if not, read your java Textbook again ;)
    joey tribbiani, May 9, 2004
    #2
    1. Advertising

  3. oh, and in Java there is no such thing as passing by reference
    joey tribbiani, May 9, 2004
    #3
  4. harry

    Arvind Guest

    Read this article on javaworld about pass-by-ref
    http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

    If your question still remains unanswered OR you are still confused -
    post a similar cheat sheet (as done in the article -so that we can get
    you to understand it !)

    Arvind


    "harry" <> wrote in message news:<btrnc.6535$>...
    > I have an object that's passed in to a function as a parameter i.e public
    > boolean getProjectTitle(ProjectHeader_DTO obj) {...}
    >
    > If I then call a method on this object inside the function i.e
    > obj.setTitle("test") then using obj.getTitle() from outside the function it
    > displays the property set correctly!
    >
    > But doing something like this inside the function doesn't -
    >
    > ProjectHeader_DTO x = new ProjectHeader_DTO();
    > x.setProjectTitle("test");
    > obj=x;
    >
    > As long as I explictly set the properties it works but why not when
    > assigning another object?
    >
    > Tried using the clone() method aswell & inside the function its works fine
    > until after the return!
    >
    > I do not want the object to be return'ed from the function!
    >
    > Any ideas?
    >
    > thanks
    >
    > harry
    Arvind, May 9, 2004
    #4
  5. harry

    Roedy Green Guest

    Roedy Green, May 9, 2004
    #5
  6. On Sun, 09 May 2004 14:44:23 GMT, harry wrote:

    > I have an object that's passed in to a function as a parameter i.e public
    > boolean getProjectTitle(ProjectHeader_DTO obj) {...}
    >
    > If I then call a method on this object inside the function i.e
    > obj.setTitle("test") then using obj.getTitle() from outside the function it
    > displays the property set correctly!
    >
    > But doing something like this inside the function doesn't -
    >
    > ProjectHeader_DTO x = new ProjectHeader_DTO();
    > x.setProjectTitle("test");
    > obj=x;
    >
    > As long as I explictly set the properties it works but why not when
    > assigning another object?


    Because you haven't defined and called any method to do the assigning. Java
    doesn't do that for you.
    Timo Kinnunen, May 10, 2004
    #6
  7. Timo Kinnunen <> scribbled the following:
    > On Sun, 09 May 2004 14:44:23 GMT, harry wrote:
    >> I have an object that's passed in to a function as a parameter i.e public
    >> boolean getProjectTitle(ProjectHeader_DTO obj) {...}
    >>
    >> If I then call a method on this object inside the function i.e
    >> obj.setTitle("test") then using obj.getTitle() from outside the function it
    >> displays the property set correctly!
    >>
    >> But doing something like this inside the function doesn't -
    >>
    >> ProjectHeader_DTO x = new ProjectHeader_DTO();
    >> x.setProjectTitle("test");
    >> obj=x;
    >>
    >> As long as I explictly set the properties it works but why not when
    >> assigning another object?


    > Because you haven't defined and called any method to do the assigning. Java
    > doesn't do that for you.


    Are you again starting with the conclusion that Java has pass by
    reference and twisting definitions to try to arrive at that conclusion?

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Roses are red, violets are blue, I'm a schitzophrenic and so am I."
    - Bob Wiley
    Joona I Palaste, May 11, 2004
    #7
  8. harry

    Tony Morris Guest

    "harry" <> wrote in message
    news:btrnc.6535$...
    > I have an object that's passed in to a function as a parameter i.e public
    > boolean getProjectTitle(ProjectHeader_DTO obj) {...}
    >
    > If I then call a method on this object inside the function i.e
    > obj.setTitle("test") then using obj.getTitle() from outside the function

    it
    > displays the property set correctly!
    >
    > But doing something like this inside the function doesn't -
    >
    > ProjectHeader_DTO x = new ProjectHeader_DTO();
    > x.setProjectTitle("test");
    > obj=x;
    >
    > As long as I explictly set the properties it works but why not when
    > assigning another object?
    >
    > Tried using the clone() method aswell & inside the function its works fine
    > until after the return!
    >
    > I do not want the object to be return'ed from the function!
    >
    > Any ideas?
    >
    > thanks
    >
    > harry
    >
    >


    Here are a few fundamental concepts that you seem to misunderstand:
    a) Java does not have functions, it has methods
    b) Java does not allow you to declare types that are objects, only object
    references, so you could not possibly be passing an object or returning one
    from a function [sic]
    c) Java is strictly pass by value, there is no pass by reference in Java

    http://www.xdweb.net/~dibblego/java/faq/answers.html#q21

    Good luck.

    --
    Tony Morris
    (BInfTech, Cert 3 I.T.)
    Software Engineer
    (2003 VTR1000F)
    Sun Certified Programmer for the Java 2 Platform (1.4)
    Sun Certified Developer for the Java 2 Platform
    Tony Morris, May 11, 2004
    #8
  9. harry

    Roedy Green Guest

    On Tue, 11 May 2004 15:17:56 +1000, "Tony Morris"
    <> wrote or quoted :

    >Here are a few fundamental concepts that you seem to misunderstand:
    >a) Java does not have functions, it has methods
    >b) Java does not allow you to declare types that are objects, only object
    >references, so you could not possibly be passing an object or returning one
    >from a function [sic]
    >c) Java is strictly pass by value, there is no pass by reference in Java


    there is no comp.lang.java newsgroup.

    see http://mindprod.com/jgloss/callbyvalue.html
    and http://mindprod.com/jgloss/callbyreference.html
    to get this all clear.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, May 11, 2004
    #9
  10. On 11 May 2004 03:35:59 GMT, Joona I Palaste wrote:

    > Timo Kinnunen <> scribbled the following:
    >> Because you haven't defined and called any method to do the assigning. Java
    >> doesn't do that for you.

    >
    > Are you again starting with the conclusion that Java has pass by
    > reference and twisting definitions to try to arrive at that conclusion?


    No, I'm answering the OP's question.
    Timo Kinnunen, May 11, 2004
    #10
  11. Timo Kinnunen <> scribbled the following:
    > On 11 May 2004 03:35:59 GMT, Joona I Palaste wrote:
    >> Timo Kinnunen <> scribbled the following:
    >>> Because you haven't defined and called any method to do the assigning. Java
    >>> doesn't do that for you.

    >>
    >> Are you again starting with the conclusion that Java has pass by
    >> reference and twisting definitions to try to arrive at that conclusion?


    > No, I'm answering the OP's question.


    In a very curious way. In Java, assigning is only done with the =
    operator. You don't "define methods to do the assigning".

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Shh! The maestro is decomposing!"
    - Gary Larson
    Joona I Palaste, May 11, 2004
    #11
  12. Joona I Palaste wrote:
    >>>>Because you haven't defined and called any method to do the assigning. Java
    >>>>doesn't do that for you.
    >>>
    >>>Are you again starting with the conclusion that Java has pass by
    >>>reference and twisting definitions to try to arrive at that conclusion?

    >
    >
    >>No, I'm answering the OP's question.

    >
    >
    > In a very curious way. In Java, assigning is only done with the =
    > operator. You don't "define methods to do the assigning".


    Isn't that exactly what set methods do?
    Michael Borgwardt, May 11, 2004
    #12
  13. Michael Borgwardt <> scribbled the following:
    > Joona I Palaste wrote:
    >>>>>Because you haven't defined and called any method to do the assigning. Java
    >>>>>doesn't do that for you.
    >>>>
    >>>>Are you again starting with the conclusion that Java has pass by
    >>>>reference and twisting definitions to try to arrive at that conclusion?

    >>
    >>>No, I'm answering the OP's question.

    >>
    >> In a very curious way. In Java, assigning is only done with the =
    >> operator. You don't "define methods to do the assigning".


    > Isn't that exactly what set methods do?


    Set methods use the = operator to actually assign the value. I'm not
    sure whether Timo meant such set methods or methods that "assign"
    something by only calling other methods.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "You can pick your friends, you can pick your nose, but you can't pick your
    relatives."
    - MAD Magazine
    Joona I Palaste, May 11, 2004
    #13
  14. On 11 May 2004 10:39:43 GMT, Joona I Palaste wrote:

    > Timo Kinnunen <> scribbled the following:
    >> No, I'm answering the OP's question.

    >
    > In a very curious way. In Java, assigning is only done with the =
    > operator. You don't "define methods to do the assigning".


    There is no assigning to an object in Java, which it seemed the OP was
    looking for. So, short of redesigning his code, the OP needs to provide and
    activate such an abstraction himself. To do that, a method "to do the
    assigning" fits the bill well.
    Timo Kinnunen, May 11, 2004
    #14
  15. Timo Kinnunen <> scribbled the following:
    > On 11 May 2004 10:39:43 GMT, Joona I Palaste wrote:
    >> Timo Kinnunen <> scribbled the following:
    >>> No, I'm answering the OP's question.

    >>
    >> In a very curious way. In Java, assigning is only done with the =
    >> operator. You don't "define methods to do the assigning".


    > There is no assigning to an object in Java, which it seemed the OP was
    > looking for. So, short of redesigning his code, the OP needs to provide and
    > activate such an abstraction himself. To do that, a method "to do the
    > assigning" fits the bill well.


    Design-wise, this might be a good approach. But it won't be "assigning"
    anything, at least not to the variable given as the method parameter.
    To avoid the equivocation fallacy, I suggest a better name for the new
    method.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "When a man talks dirty to a woman, that's sexual harassment. When a woman talks
    dirty to a man, that's 14.99 per minute + local telephone charges!"
    - Ruben Stiller
    Joona I Palaste, May 11, 2004
    #15
  16. On 11 May 2004 14:09:48 GMT, Joona I Palaste wrote:

    > Design-wise, this might be a good approach. But it won't be "assigning"
    > anything, at least not to the variable given as the method parameter.
    > To avoid the equivocation fallacy, I suggest a better name for the new
    > method.


    But I didn't suggest any method name, I just described the abstraction :)
    Timo Kinnunen, May 11, 2004
    #16
  17. harry

    Roedy Green Guest

    On Tue, 11 May 2004 12:51:49 +0200, Michael Borgwardt
    <> wrote or quoted :

    >Isn't that exactly what set methods do?


    Indirectly. The actual assignment is done inside the method with =.

    there is nothing magic about a set method other than the beanbox
    exposes them to the public.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, May 11, 2004
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sam pal
    Replies:
    3
    Views:
    537
    E. Robert Tisdale
    Jul 16, 2003
  2. Michael
    Replies:
    4
    Views:
    408
    Matt Hammond
    Jun 26, 2006
  3. Jeff

    more auto_ptr confusion

    Jeff, May 17, 2006, in forum: C++
    Replies:
    23
    Views:
    870
    Noah Roberts
    May 19, 2006
  4. Robert Klemme

    With a Ruby Yell: more, more more!

    Robert Klemme, Sep 28, 2005, in forum: Ruby
    Replies:
    5
    Views:
    215
    Jeff Wood
    Sep 29, 2005
  5. Replies:
    2
    Views:
    144
Loading...

Share This Page