Passing by reference, more confusion...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • harry

    Passing by reference, more confusion...

    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("t est") 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_D TO x = new ProjectHeader_D TO();
    x.setProjectTit le("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


  • joey tribbiani

    #2
    Re: Passing by reference, more confusion...

    harry wrote:[color=blue]
    > 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("t est") 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_D TO x = new ProjectHeader_D TO();
    > x.setProjectTit le("test");
    > obj=x;
    >
    > As long as I explictly set the properties it works but why not when
    > assigning another object?[/color]

    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("t est") 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_hous e_nice_and_clea n(); // 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_hous e_nice_and_clea n(); // 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 ;)

    Comment

    • joey tribbiani

      #3
      Re: Passing by reference, more confusion...

      oh, and in Java there is no such thing as passing by reference

      Comment

      • Arvind

        #4
        Re: Passing by reference, more confusion...

        Read this article on javaworld about pass-by-ref
        Business technology, IT news, product reviews and enterprise IT strategies.


        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" <a@abc.com> wrote in message news:<btrnc.653 5$Bx5.62333106@ news-text.cableinet. net>...[color=blue]
        > 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("t est") 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_D TO x = new ProjectHeader_D TO();
        > x.setProjectTit le("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[/color]

        Comment

        • Roedy Green

          #5
          Re: Passing by reference, more confusion...

          see http://mindprod.com/jgloss/callbyvalue.html


          --
          Canadian Mind Products, Roedy Green.
          Coaching, problem solving, economical contract programming.
          See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

          Comment

          • Timo Kinnunen

            #6
            Re: Passing by reference, more confusion...

            On Sun, 09 May 2004 14:44:23 GMT, harry wrote:
            [color=blue]
            > 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("t est") 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_D TO x = new ProjectHeader_D TO();
            > x.setProjectTit le("test");
            > obj=x;
            >
            > As long as I explictly set the properties it works but why not when
            > assigning another object?[/color]

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

            Comment

            • Joona I Palaste

              #7
              Re: Passing by reference, more confusion...

              Timo Kinnunen <timo.kinnunen@ bigfoot.com> scribbled the following:[color=blue]
              > On Sun, 09 May 2004 14:44:23 GMT, harry wrote:[color=green]
              >> 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("t est") 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_D TO x = new ProjectHeader_D TO();
              >> x.setProjectTit le("test");
              >> obj=x;
              >>
              >> As long as I explictly set the properties it works but why not when
              >> assigning another object?[/color][/color]
              [color=blue]
              > Because you haven't defined and called any method to do the assigning. Java
              > doesn't do that for you.[/color]

              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 (palaste@cc.hel sinki.fi) ------------- Finland --------\
              \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
              "Roses are red, violets are blue, I'm a schitzophrenic and so am I."
              - Bob Wiley

              Comment

              • Tony Morris

                #8
                Re: Passing by reference, more confusion...

                "harry" <a@abc.com> wrote in message
                news:btrnc.6535 $Bx5.62333106@n ews-text.cableinet. net...[color=blue]
                > 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("t est") then using obj.getTitle() from outside the function[/color]
                it[color=blue]
                > displays the property set correctly!
                >
                > But doing something like this inside the function doesn't -
                >
                > ProjectHeader_D TO x = new ProjectHeader_D TO();
                > x.setProjectTit le("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
                >
                >[/color]

                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



                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



                Comment

                • Timo Kinnunen

                  #9
                  Re: Passing by reference, more confusion...

                  On 11 May 2004 03:35:59 GMT, Joona I Palaste wrote:
                  [color=blue]
                  > Timo Kinnunen <timo.kinnunen@ bigfoot.com> scribbled the following:[color=green]
                  >> Because you haven't defined and called any method to do the assigning. Java
                  >> doesn't do that for you.[/color]
                  >
                  > Are you again starting with the conclusion that Java has pass by
                  > reference and twisting definitions to try to arrive at that conclusion?[/color]

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

                  Comment

                  • Joona I Palaste

                    #10
                    Re: Passing by reference, more confusion...

                    Timo Kinnunen <timo.kinnunen@ bigfoot.com> scribbled the following:[color=blue]
                    > On 11 May 2004 03:35:59 GMT, Joona I Palaste wrote:[color=green]
                    >> Timo Kinnunen <timo.kinnunen@ bigfoot.com> scribbled the following:[color=darkred]
                    >>> Because you haven't defined and called any method to do the assigning. Java
                    >>> doesn't do that for you.[/color]
                    >>
                    >> Are you again starting with the conclusion that Java has pass by
                    >> reference and twisting definitions to try to arrive at that conclusion?[/color][/color]
                    [color=blue]
                    > No, I'm answering the OP's question.[/color]

                    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 (palaste@cc.hel sinki.fi) ------------- Finland --------\
                    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
                    "Shh! The maestro is decomposing!"
                    - Gary Larson

                    Comment

                    • Michael Borgwardt

                      #11
                      Re: Passing by reference, more confusion...

                      Joona I Palaste wrote:[color=blue][color=green][color=darkred]
                      >>>>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?[/color][/color]
                      >
                      >[color=green]
                      >>No, I'm answering the OP's question.[/color]
                      >
                      >
                      > In a very curious way. In Java, assigning is only done with the =
                      > operator. You don't "define methods to do the assigning".[/color]

                      Isn't that exactly what set methods do?

                      Comment

                      • Joona I Palaste

                        #12
                        Re: Passing by reference, more confusion...

                        Michael Borgwardt <brazil@brazi ls-animeland.de> scribbled the following:[color=blue]
                        > Joona I Palaste wrote:[color=green][color=darkred]
                        >>>>>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?[/color]
                        >>[color=darkred]
                        >>>No, I'm answering the OP's question.[/color]
                        >>
                        >> In a very curious way. In Java, assigning is only done with the =
                        >> operator. You don't "define methods to do the assigning".[/color][/color]
                        [color=blue]
                        > Isn't that exactly what set methods do?[/color]

                        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 (palaste@cc.hel sinki.fi) ------------- 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

                        Comment

                        • Roedy Green

                          #13
                          Re: Passing by reference, more confusion...

                          On Tue, 11 May 2004 12:51:49 +0200, Michael Borgwardt
                          <brazil@brazi ls-animeland.de> wrote or quoted :
                          [color=blue]
                          >Isn't that exactly what set methods do?[/color]

                          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.

                          Comment

                          Working...