How do the instance variables effect the values of my constructors here?
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
add a comment |
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
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
add a comment |
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
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
java variables constructor
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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
.
add a comment |
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
.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
.
add a comment |
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
.
add a comment |
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
.
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
.
answered Nov 13 '18 at 17:14
Jean-Baptiste YunèsJean-Baptiste Yunès
23.2k12652
23.2k12652
add a comment |
add a comment |
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
.
add a comment |
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
.
add a comment |
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
.
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
.
answered Nov 13 '18 at 17:26
forpasforpas
11k2423
11k2423
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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