9 Replies Latest reply: Feb 28, 2013 11:49 AM by 993288 RSS

    Dumfounded by this

      Hello, I need some help with this piece of code:

                               OrderDetail tempDet = oDet;
                               System.out.println(oDet.symbol + " before " + oDet.orderRefNum);
                               tempDet.symbol = MarketPrices.marketPrices.get(c).symbol;
                               System.out.println(oDet.symbol + " after " + oDet.orderRefNum);

      OrderDetail is a class with only fields.

      oDet.symbol changes after those lines of code are run.

      I can not figure out for the life of me....

      Edited by: 990285 on Feb 25, 2013 4:13 PM
        • 1. Re: Dumfounded by this
          // oDet is a reference to an OrderDetail object (emphasis on IS A REFERENCE TO)
          OrderDetail tempDet = oDet; 
          // Now tempDet is a reference to the same OrderDetail object
          System.out.println(oDet.symbol + " before " + oDet.orderRefNum);
          tempDet.symbol = MarketPrices.marketPrices.get(c).symbol;
          // Via the tempDet reference, we just changed the value of the OrderDetail field 'symbol'
          System.out.println(oDet.symbol + " after " + oDet.orderRefNum);
          // Therefore we just printed the new value, because the same OrderDetail object happened to be REFERED TO by two different variables
          Edited by: baftos on Feb 25, 2013 7:35 PM
          • 2. Re: Dumfounded by this

            Your oDet and tempDet are the references of the same OrderDetail instance. Hence the change reflects on both the references.


            • 3. Re: Dumfounded by this
              Ah the old reference VS instance confusion. If you make that (understandable) mistake you should be posting in New to Java.

              Think of it like this:

              Book - its a class

              Harry Potter - its an instance of Book (Book book = new Book("harry potter"))

              A note in your phone mentioning that the harry potter book is at your parents - its a reference to the Book instance
              A note in your email mentioning that the harry potter book is at your parents - its another reference to the same Book instance!

              A bit of imagination needed to compare a real world note to a variable in programming terms ;)
              • 4. Re: Dumfounded by this
                Thank you very much everyone for the help.

                I am still slightly confused, this code is in a method:

                public static ArrayList<OrderDetail> spotSplit(OrderDetail oDet)

                so i don't understand how changing a reference to tempDet changes oDet. I understand that I am referencing a new symbol field (in a different class type, but symbol is just a string) for tempDet.

                For that matter when I run the following:

                public static void main(String[] args)
                          String a = "5";

                     public static void test(String d)
                          String b = d;
                          System.out.println(b + " " + d);
                          String c = "7";
                          b = c;
                          System.out.println(b + " " + d);

                d remains 5.

                Sorry if I should have posted this in new to java.
                • 5. Re: Dumfounded by this
                  Yes, you never assign a different object to variable d. You only change the references b and c.
                  • 6. Re: Dumfounded by this
                    Ok you have a TV1 and it's remote control called d.
                    Next TV1 gets a second remote control called b.
                    Now you get a TV2 with remote control c
                    Now you reprogram remote b to control TV2.

                    -> Why should remote d control TV2 as well?

                    Got it?
                    • 7. Re: Dumfounded by this

                      In your case initially d is 5 as set in the main method.
                      Then you are assigning d to b. Now b and d have value 5.
                      Then you are creating a new String c with value 7. Now b and d have 5 and c has 7.
                      Then you are assigning c to b. Now b and c have 7 and d has 5.

                      In the whole operation you have not modified d hence d remains with whatever you have passed to the test method. Hence d stays 5.


                      • 8. Re: Dumfounded by this
                        I love that remote control analogy! I gotta remember that next time I'm mentoring a newbie!
                        • 9. Re: Dumfounded by this
                          In the Remote Control answer,

                          I am expecting remote d to be unchanged, and it is. I don't expect it to program tv2, i expect it it program tv1, which is what I told it to.

                          In the other example, it does change. I never have oDet = anything. I have tempDet = oDet. When I change tempDet, oDet changes. That is the part that I am not understanding.

                          Thank you again everyone for your help. I solved this by making a constructer in OrderDetail that is public OrderDetail(OrderDetail oD) and copies the values.

                          Edited by: 990285 on Feb 28, 2013 9:48 AM