How do the instance variables effect the values of my constructors here?










0















I am working through a java exercise and am somewhat confused what the new values of the last 4 lines of code are.



public class Snake 
public Snake x = null;
public Snake y = null;

public static void main(String args)
Snake a = new Snake();
Snake b = new Snake();
a.x = a;
b.x = a.x;
b.y = b.x;
a.y = b.y.x;











share|improve this question
























  • You just have an object with a property that points to the object itself... and that's a little like the beginning of infinity :-)

    – ernest_k
    Nov 13 '18 at 17:09











  • By the end both X and Y fields of A and B are equal to A. Whoever authored this exercise is an idiot. It's like trying to teach someone English with "how much wood would a woodchuck chuck"

    – Michael
    Nov 13 '18 at 17:12
















0















I am working through a java exercise and am somewhat confused what the new values of the last 4 lines of code are.



public class Snake 
public Snake x = null;
public Snake y = null;

public static void main(String args)
Snake a = new Snake();
Snake b = new Snake();
a.x = a;
b.x = a.x;
b.y = b.x;
a.y = b.y.x;











share|improve this question
























  • You just have an object with a property that points to the object itself... and that's a little like the beginning of infinity :-)

    – ernest_k
    Nov 13 '18 at 17:09











  • By the end both X and Y fields of A and B are equal to A. Whoever authored this exercise is an idiot. It's like trying to teach someone English with "how much wood would a woodchuck chuck"

    – Michael
    Nov 13 '18 at 17:12














0












0








0








I am working through a java exercise and am somewhat confused what the new values of the last 4 lines of code are.



public class Snake 
public Snake x = null;
public Snake y = null;

public static void main(String args)
Snake a = new Snake();
Snake b = new Snake();
a.x = a;
b.x = a.x;
b.y = b.x;
a.y = b.y.x;











share|improve this question
















I am working through a java exercise and am somewhat confused what the new values of the last 4 lines of code are.



public class Snake 
public Snake x = null;
public Snake y = null;

public static void main(String args)
Snake a = new Snake();
Snake b = new Snake();
a.x = a;
b.x = a.x;
b.y = b.x;
a.y = b.y.x;








java variables constructor






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 17:09









Michael

19.6k83470




19.6k83470










asked Nov 13 '18 at 17:05









Salizmo SqueegeeSalizmo Squeegee

22




22












  • You just have an object with a property that points to the object itself... and that's a little like the beginning of infinity :-)

    – ernest_k
    Nov 13 '18 at 17:09











  • By the end both X and Y fields of A and B are equal to A. Whoever authored this exercise is an idiot. It's like trying to teach someone English with "how much wood would a woodchuck chuck"

    – Michael
    Nov 13 '18 at 17:12


















  • You just have an object with a property that points to the object itself... and that's a little like the beginning of infinity :-)

    – ernest_k
    Nov 13 '18 at 17:09











  • By the end both X and Y fields of A and B are equal to A. Whoever authored this exercise is an idiot. It's like trying to teach someone English with "how much wood would a woodchuck chuck"

    – Michael
    Nov 13 '18 at 17:12

















You just have an object with a property that points to the object itself... and that's a little like the beginning of infinity :-)

– ernest_k
Nov 13 '18 at 17:09





You just have an object with a property that points to the object itself... and that's a little like the beginning of infinity :-)

– ernest_k
Nov 13 '18 at 17:09













By the end both X and Y fields of A and B are equal to A. Whoever authored this exercise is an idiot. It's like trying to teach someone English with "how much wood would a woodchuck chuck"

– Michael
Nov 13 '18 at 17:12






By the end both X and Y fields of A and B are equal to A. Whoever authored this exercise is an idiot. It's like trying to teach someone English with "how much wood would a woodchuck chuck"

– Michael
Nov 13 '18 at 17:12













2 Answers
2






active

oldest

votes


















1














Then you understood that a and b are two snakes. But they are really references to two different snakes.



a.x = a : means make a.x refer to snake referred by a.



b.x = a.x : means make b.x refer to snake referred by a.x (so the same as a).



b.y = b.x : means make b.y refer to snake referred by b.x (so the same as a).



a.y = b.y.x: means make a.y refer to snake referred by b.y.x, as b.y refers to a, this refers to a.x which is the same as a.






share|improve this answer






























    0














    This is how it works:



    a.x = a;


    the x Snake object (property) of the a Snake object becomes a reference (points) to the a Snake object itself.



    b.x = a.x;


    the x Snake object (property) of the b Snake object becomes a reference (points) to the a.x Snake object which previously was set to refer to the a Snake object.
    So this b.x also becomes a reference (points) to the a Snake object .



    b.y = b.x;


    the y Snake object (property) of the b Snake object becomes a reference (points) to the b.x Snake object which previously was set to refer to the a Snake object.
    So this b.y also becomes a reference (points) to the a Snake object .



    a.y = b.y.x;


    the y Snake object (property) of the a Snake object becomes a reference (points) to the b.y.x Snake object and since b.y was previously set to a it is equivalent to a.x which previously was set to refer to the a Snake object.
    So this a.y also becomes a reference (points) to the a Snake object .


    Finally: all the left parts of the assignments refer (point) to a.






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286149%2fhow-do-the-instance-variables-effect-the-values-of-my-constructors-here%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Then you understood that a and b are two snakes. But they are really references to two different snakes.



      a.x = a : means make a.x refer to snake referred by a.



      b.x = a.x : means make b.x refer to snake referred by a.x (so the same as a).



      b.y = b.x : means make b.y refer to snake referred by b.x (so the same as a).



      a.y = b.y.x: means make a.y refer to snake referred by b.y.x, as b.y refers to a, this refers to a.x which is the same as a.






      share|improve this answer



























        1














        Then you understood that a and b are two snakes. But they are really references to two different snakes.



        a.x = a : means make a.x refer to snake referred by a.



        b.x = a.x : means make b.x refer to snake referred by a.x (so the same as a).



        b.y = b.x : means make b.y refer to snake referred by b.x (so the same as a).



        a.y = b.y.x: means make a.y refer to snake referred by b.y.x, as b.y refers to a, this refers to a.x which is the same as a.






        share|improve this answer

























          1












          1








          1







          Then you understood that a and b are two snakes. But they are really references to two different snakes.



          a.x = a : means make a.x refer to snake referred by a.



          b.x = a.x : means make b.x refer to snake referred by a.x (so the same as a).



          b.y = b.x : means make b.y refer to snake referred by b.x (so the same as a).



          a.y = b.y.x: means make a.y refer to snake referred by b.y.x, as b.y refers to a, this refers to a.x which is the same as a.






          share|improve this answer













          Then you understood that a and b are two snakes. But they are really references to two different snakes.



          a.x = a : means make a.x refer to snake referred by a.



          b.x = a.x : means make b.x refer to snake referred by a.x (so the same as a).



          b.y = b.x : means make b.y refer to snake referred by b.x (so the same as a).



          a.y = b.y.x: means make a.y refer to snake referred by b.y.x, as b.y refers to a, this refers to a.x which is the same as a.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 17:14









          Jean-Baptiste YunèsJean-Baptiste Yunès

          23.2k12652




          23.2k12652























              0














              This is how it works:



              a.x = a;


              the x Snake object (property) of the a Snake object becomes a reference (points) to the a Snake object itself.



              b.x = a.x;


              the x Snake object (property) of the b Snake object becomes a reference (points) to the a.x Snake object which previously was set to refer to the a Snake object.
              So this b.x also becomes a reference (points) to the a Snake object .



              b.y = b.x;


              the y Snake object (property) of the b Snake object becomes a reference (points) to the b.x Snake object which previously was set to refer to the a Snake object.
              So this b.y also becomes a reference (points) to the a Snake object .



              a.y = b.y.x;


              the y Snake object (property) of the a Snake object becomes a reference (points) to the b.y.x Snake object and since b.y was previously set to a it is equivalent to a.x which previously was set to refer to the a Snake object.
              So this a.y also becomes a reference (points) to the a Snake object .


              Finally: all the left parts of the assignments refer (point) to a.






              share|improve this answer



























                0














                This is how it works:



                a.x = a;


                the x Snake object (property) of the a Snake object becomes a reference (points) to the a Snake object itself.



                b.x = a.x;


                the x Snake object (property) of the b Snake object becomes a reference (points) to the a.x Snake object which previously was set to refer to the a Snake object.
                So this b.x also becomes a reference (points) to the a Snake object .



                b.y = b.x;


                the y Snake object (property) of the b Snake object becomes a reference (points) to the b.x Snake object which previously was set to refer to the a Snake object.
                So this b.y also becomes a reference (points) to the a Snake object .



                a.y = b.y.x;


                the y Snake object (property) of the a Snake object becomes a reference (points) to the b.y.x Snake object and since b.y was previously set to a it is equivalent to a.x which previously was set to refer to the a Snake object.
                So this a.y also becomes a reference (points) to the a Snake object .


                Finally: all the left parts of the assignments refer (point) to a.






                share|improve this answer

























                  0












                  0








                  0







                  This is how it works:



                  a.x = a;


                  the x Snake object (property) of the a Snake object becomes a reference (points) to the a Snake object itself.



                  b.x = a.x;


                  the x Snake object (property) of the b Snake object becomes a reference (points) to the a.x Snake object which previously was set to refer to the a Snake object.
                  So this b.x also becomes a reference (points) to the a Snake object .



                  b.y = b.x;


                  the y Snake object (property) of the b Snake object becomes a reference (points) to the b.x Snake object which previously was set to refer to the a Snake object.
                  So this b.y also becomes a reference (points) to the a Snake object .



                  a.y = b.y.x;


                  the y Snake object (property) of the a Snake object becomes a reference (points) to the b.y.x Snake object and since b.y was previously set to a it is equivalent to a.x which previously was set to refer to the a Snake object.
                  So this a.y also becomes a reference (points) to the a Snake object .


                  Finally: all the left parts of the assignments refer (point) to a.






                  share|improve this answer













                  This is how it works:



                  a.x = a;


                  the x Snake object (property) of the a Snake object becomes a reference (points) to the a Snake object itself.



                  b.x = a.x;


                  the x Snake object (property) of the b Snake object becomes a reference (points) to the a.x Snake object which previously was set to refer to the a Snake object.
                  So this b.x also becomes a reference (points) to the a Snake object .



                  b.y = b.x;


                  the y Snake object (property) of the b Snake object becomes a reference (points) to the b.x Snake object which previously was set to refer to the a Snake object.
                  So this b.y also becomes a reference (points) to the a Snake object .



                  a.y = b.y.x;


                  the y Snake object (property) of the a Snake object becomes a reference (points) to the b.y.x Snake object and since b.y was previously set to a it is equivalent to a.x which previously was set to refer to the a Snake object.
                  So this a.y also becomes a reference (points) to the a Snake object .


                  Finally: all the left parts of the assignments refer (point) to a.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 17:26









                  forpasforpas

                  11k2423




                  11k2423



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286149%2fhow-do-the-instance-variables-effect-the-values-of-my-constructors-here%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Museum of Modern and Contemporary Art of Trento and Rovereto

                      In R, how to develop a multiplot heatmap.2 figure showing key labels successfully