Assigning a Variable the Identity of Another
I have an Item class. Each Item object is held in an instance of the ItemNode class. ItemNode is an inner class in my CustomList class.
My Item class has an attribute named amount. This refers to how many of that type of Item are owned by the user
My ItemNode class also has an attribute named amount. I want the ItemNode's amount attribute to always equal the amount attribute of the Item object it holds.
In other words, (ItemNode.amount == ItemNode.item.amount)
, should always be true, even if I change the value of itemNode.amount
later on.
How can I make Java have the same identity for both ItemNode.amount
and Item.amount
?
My ItemNode Class:
/**
* Creates nodes to hold Item objects.
*/
private class ItemNode {
// the object being held by the node
private Item item;
// The type of the object
private String typeName;
// How many are owned by the player
private int amount;
// What the item-subclass's name is
private String itemName;
// the node after this
private ItemNode next;
ItemNode(Item item) {
this.data = item;
this.typeName = typeName;
this.itemName = item.getItemName();
this.amount = item.getAmount();
this.next = null;
}
}
java pass-by-reference identity
add a comment |
I have an Item class. Each Item object is held in an instance of the ItemNode class. ItemNode is an inner class in my CustomList class.
My Item class has an attribute named amount. This refers to how many of that type of Item are owned by the user
My ItemNode class also has an attribute named amount. I want the ItemNode's amount attribute to always equal the amount attribute of the Item object it holds.
In other words, (ItemNode.amount == ItemNode.item.amount)
, should always be true, even if I change the value of itemNode.amount
later on.
How can I make Java have the same identity for both ItemNode.amount
and Item.amount
?
My ItemNode Class:
/**
* Creates nodes to hold Item objects.
*/
private class ItemNode {
// the object being held by the node
private Item item;
// The type of the object
private String typeName;
// How many are owned by the player
private int amount;
// What the item-subclass's name is
private String itemName;
// the node after this
private ItemNode next;
ItemNode(Item item) {
this.data = item;
this.typeName = typeName;
this.itemName = item.getItemName();
this.amount = item.getAmount();
this.next = null;
}
}
java pass-by-reference identity
1
1) DON'T give ItemNode an amount field. Instead simply have yourgetAmount()
method from ItemNode return the value from the item it holds. Nothing more, nothing less. The Decorator design pattern may be what you're looking for.
– Hovercraft Full Of Eels
Nov 26 '18 at 1:49
But that won't let me changeItemNode.amount
andItem.amount
at the same time.
– LuminousNutria
Nov 26 '18 at 1:50
1
That's because you shouldn't be changing both, nor should you have both.
– Hovercraft Full Of Eels
Nov 26 '18 at 1:51
add a comment |
I have an Item class. Each Item object is held in an instance of the ItemNode class. ItemNode is an inner class in my CustomList class.
My Item class has an attribute named amount. This refers to how many of that type of Item are owned by the user
My ItemNode class also has an attribute named amount. I want the ItemNode's amount attribute to always equal the amount attribute of the Item object it holds.
In other words, (ItemNode.amount == ItemNode.item.amount)
, should always be true, even if I change the value of itemNode.amount
later on.
How can I make Java have the same identity for both ItemNode.amount
and Item.amount
?
My ItemNode Class:
/**
* Creates nodes to hold Item objects.
*/
private class ItemNode {
// the object being held by the node
private Item item;
// The type of the object
private String typeName;
// How many are owned by the player
private int amount;
// What the item-subclass's name is
private String itemName;
// the node after this
private ItemNode next;
ItemNode(Item item) {
this.data = item;
this.typeName = typeName;
this.itemName = item.getItemName();
this.amount = item.getAmount();
this.next = null;
}
}
java pass-by-reference identity
I have an Item class. Each Item object is held in an instance of the ItemNode class. ItemNode is an inner class in my CustomList class.
My Item class has an attribute named amount. This refers to how many of that type of Item are owned by the user
My ItemNode class also has an attribute named amount. I want the ItemNode's amount attribute to always equal the amount attribute of the Item object it holds.
In other words, (ItemNode.amount == ItemNode.item.amount)
, should always be true, even if I change the value of itemNode.amount
later on.
How can I make Java have the same identity for both ItemNode.amount
and Item.amount
?
My ItemNode Class:
/**
* Creates nodes to hold Item objects.
*/
private class ItemNode {
// the object being held by the node
private Item item;
// The type of the object
private String typeName;
// How many are owned by the player
private int amount;
// What the item-subclass's name is
private String itemName;
// the node after this
private ItemNode next;
ItemNode(Item item) {
this.data = item;
this.typeName = typeName;
this.itemName = item.getItemName();
this.amount = item.getAmount();
this.next = null;
}
}
java pass-by-reference identity
java pass-by-reference identity
edited Nov 26 '18 at 1:51
LuminousNutria
asked Nov 26 '18 at 1:45
LuminousNutriaLuminousNutria
808324
808324
1
1) DON'T give ItemNode an amount field. Instead simply have yourgetAmount()
method from ItemNode return the value from the item it holds. Nothing more, nothing less. The Decorator design pattern may be what you're looking for.
– Hovercraft Full Of Eels
Nov 26 '18 at 1:49
But that won't let me changeItemNode.amount
andItem.amount
at the same time.
– LuminousNutria
Nov 26 '18 at 1:50
1
That's because you shouldn't be changing both, nor should you have both.
– Hovercraft Full Of Eels
Nov 26 '18 at 1:51
add a comment |
1
1) DON'T give ItemNode an amount field. Instead simply have yourgetAmount()
method from ItemNode return the value from the item it holds. Nothing more, nothing less. The Decorator design pattern may be what you're looking for.
– Hovercraft Full Of Eels
Nov 26 '18 at 1:49
But that won't let me changeItemNode.amount
andItem.amount
at the same time.
– LuminousNutria
Nov 26 '18 at 1:50
1
That's because you shouldn't be changing both, nor should you have both.
– Hovercraft Full Of Eels
Nov 26 '18 at 1:51
1
1
1) DON'T give ItemNode an amount field. Instead simply have your
getAmount()
method from ItemNode return the value from the item it holds. Nothing more, nothing less. The Decorator design pattern may be what you're looking for.– Hovercraft Full Of Eels
Nov 26 '18 at 1:49
1) DON'T give ItemNode an amount field. Instead simply have your
getAmount()
method from ItemNode return the value from the item it holds. Nothing more, nothing less. The Decorator design pattern may be what you're looking for.– Hovercraft Full Of Eels
Nov 26 '18 at 1:49
But that won't let me change
ItemNode.amount
and Item.amount
at the same time.– LuminousNutria
Nov 26 '18 at 1:50
But that won't let me change
ItemNode.amount
and Item.amount
at the same time.– LuminousNutria
Nov 26 '18 at 1:50
1
1
That's because you shouldn't be changing both, nor should you have both.
– Hovercraft Full Of Eels
Nov 26 '18 at 1:51
That's because you shouldn't be changing both, nor should you have both.
– Hovercraft Full Of Eels
Nov 26 '18 at 1:51
add a comment |
1 Answer
1
active
oldest
votes
Don't give your ItemNode class an amount field as by doing this you're creating "parallel fields" and must take pains to make sure that they stay in sync, when in fact they can fall out of sync easily. Instead and much more simply give your ItemNode class a public getAmount()
method that simply calls and returns its item's getAmount()
method. Same if you need setter methods. Remember to make your code as idiot-proof as possible. Also look into the Decorator design pattern as this problem seems to be partially solved by this.
public interface Amountable {
int getAmount();
void setAmount(int amount);
}
public class Item implements Amountable {
private int amount;
public Item(int amount) {
this.amount = amount;
}
@Override
public int getAmount() {
return amount;
}
@Override
public void setAmount(int amount) {
this.amount = amount;
}
}
public class ItemNode<T extends Amountable> implements Amountable {
private T item;
public ItemNode(T item) {
this.item = item;
}
@Override
public int getAmount() {
return item.getAmount();
}
@Override
public void setAmount(int amount) {
item.setAmount(amount);
}
public T getItem() {
return item;
}
}
GM Mr Eels. Unrelated question: I see that your activity profile shows 61K "votes cast". Just wondering: my understanding is that this number shows all votes that were cast ... on content that wasnt deleted. Now I am curious how you ever got to that number? I did a lot of close reviews (from the queue) the other week, but it seems that many of these items get later ... closed, and deleted. And therefore that counter is growing like really slowly. In other words: do you have some specific "pattern"?
– GhostCat
Nov 26 '18 at 13:46
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%2f53473789%2fassigning-a-variable-the-identity-of-another%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Don't give your ItemNode class an amount field as by doing this you're creating "parallel fields" and must take pains to make sure that they stay in sync, when in fact they can fall out of sync easily. Instead and much more simply give your ItemNode class a public getAmount()
method that simply calls and returns its item's getAmount()
method. Same if you need setter methods. Remember to make your code as idiot-proof as possible. Also look into the Decorator design pattern as this problem seems to be partially solved by this.
public interface Amountable {
int getAmount();
void setAmount(int amount);
}
public class Item implements Amountable {
private int amount;
public Item(int amount) {
this.amount = amount;
}
@Override
public int getAmount() {
return amount;
}
@Override
public void setAmount(int amount) {
this.amount = amount;
}
}
public class ItemNode<T extends Amountable> implements Amountable {
private T item;
public ItemNode(T item) {
this.item = item;
}
@Override
public int getAmount() {
return item.getAmount();
}
@Override
public void setAmount(int amount) {
item.setAmount(amount);
}
public T getItem() {
return item;
}
}
GM Mr Eels. Unrelated question: I see that your activity profile shows 61K "votes cast". Just wondering: my understanding is that this number shows all votes that were cast ... on content that wasnt deleted. Now I am curious how you ever got to that number? I did a lot of close reviews (from the queue) the other week, but it seems that many of these items get later ... closed, and deleted. And therefore that counter is growing like really slowly. In other words: do you have some specific "pattern"?
– GhostCat
Nov 26 '18 at 13:46
add a comment |
Don't give your ItemNode class an amount field as by doing this you're creating "parallel fields" and must take pains to make sure that they stay in sync, when in fact they can fall out of sync easily. Instead and much more simply give your ItemNode class a public getAmount()
method that simply calls and returns its item's getAmount()
method. Same if you need setter methods. Remember to make your code as idiot-proof as possible. Also look into the Decorator design pattern as this problem seems to be partially solved by this.
public interface Amountable {
int getAmount();
void setAmount(int amount);
}
public class Item implements Amountable {
private int amount;
public Item(int amount) {
this.amount = amount;
}
@Override
public int getAmount() {
return amount;
}
@Override
public void setAmount(int amount) {
this.amount = amount;
}
}
public class ItemNode<T extends Amountable> implements Amountable {
private T item;
public ItemNode(T item) {
this.item = item;
}
@Override
public int getAmount() {
return item.getAmount();
}
@Override
public void setAmount(int amount) {
item.setAmount(amount);
}
public T getItem() {
return item;
}
}
GM Mr Eels. Unrelated question: I see that your activity profile shows 61K "votes cast". Just wondering: my understanding is that this number shows all votes that were cast ... on content that wasnt deleted. Now I am curious how you ever got to that number? I did a lot of close reviews (from the queue) the other week, but it seems that many of these items get later ... closed, and deleted. And therefore that counter is growing like really slowly. In other words: do you have some specific "pattern"?
– GhostCat
Nov 26 '18 at 13:46
add a comment |
Don't give your ItemNode class an amount field as by doing this you're creating "parallel fields" and must take pains to make sure that they stay in sync, when in fact they can fall out of sync easily. Instead and much more simply give your ItemNode class a public getAmount()
method that simply calls and returns its item's getAmount()
method. Same if you need setter methods. Remember to make your code as idiot-proof as possible. Also look into the Decorator design pattern as this problem seems to be partially solved by this.
public interface Amountable {
int getAmount();
void setAmount(int amount);
}
public class Item implements Amountable {
private int amount;
public Item(int amount) {
this.amount = amount;
}
@Override
public int getAmount() {
return amount;
}
@Override
public void setAmount(int amount) {
this.amount = amount;
}
}
public class ItemNode<T extends Amountable> implements Amountable {
private T item;
public ItemNode(T item) {
this.item = item;
}
@Override
public int getAmount() {
return item.getAmount();
}
@Override
public void setAmount(int amount) {
item.setAmount(amount);
}
public T getItem() {
return item;
}
}
Don't give your ItemNode class an amount field as by doing this you're creating "parallel fields" and must take pains to make sure that they stay in sync, when in fact they can fall out of sync easily. Instead and much more simply give your ItemNode class a public getAmount()
method that simply calls and returns its item's getAmount()
method. Same if you need setter methods. Remember to make your code as idiot-proof as possible. Also look into the Decorator design pattern as this problem seems to be partially solved by this.
public interface Amountable {
int getAmount();
void setAmount(int amount);
}
public class Item implements Amountable {
private int amount;
public Item(int amount) {
this.amount = amount;
}
@Override
public int getAmount() {
return amount;
}
@Override
public void setAmount(int amount) {
this.amount = amount;
}
}
public class ItemNode<T extends Amountable> implements Amountable {
private T item;
public ItemNode(T item) {
this.item = item;
}
@Override
public int getAmount() {
return item.getAmount();
}
@Override
public void setAmount(int amount) {
item.setAmount(amount);
}
public T getItem() {
return item;
}
}
edited Nov 26 '18 at 1:57
answered Nov 26 '18 at 1:51
Hovercraft Full Of EelsHovercraft Full Of Eels
262k20213319
262k20213319
GM Mr Eels. Unrelated question: I see that your activity profile shows 61K "votes cast". Just wondering: my understanding is that this number shows all votes that were cast ... on content that wasnt deleted. Now I am curious how you ever got to that number? I did a lot of close reviews (from the queue) the other week, but it seems that many of these items get later ... closed, and deleted. And therefore that counter is growing like really slowly. In other words: do you have some specific "pattern"?
– GhostCat
Nov 26 '18 at 13:46
add a comment |
GM Mr Eels. Unrelated question: I see that your activity profile shows 61K "votes cast". Just wondering: my understanding is that this number shows all votes that were cast ... on content that wasnt deleted. Now I am curious how you ever got to that number? I did a lot of close reviews (from the queue) the other week, but it seems that many of these items get later ... closed, and deleted. And therefore that counter is growing like really slowly. In other words: do you have some specific "pattern"?
– GhostCat
Nov 26 '18 at 13:46
GM Mr Eels. Unrelated question: I see that your activity profile shows 61K "votes cast". Just wondering: my understanding is that this number shows all votes that were cast ... on content that wasnt deleted. Now I am curious how you ever got to that number? I did a lot of close reviews (from the queue) the other week, but it seems that many of these items get later ... closed, and deleted. And therefore that counter is growing like really slowly. In other words: do you have some specific "pattern"?
– GhostCat
Nov 26 '18 at 13:46
GM Mr Eels. Unrelated question: I see that your activity profile shows 61K "votes cast". Just wondering: my understanding is that this number shows all votes that were cast ... on content that wasnt deleted. Now I am curious how you ever got to that number? I did a lot of close reviews (from the queue) the other week, but it seems that many of these items get later ... closed, and deleted. And therefore that counter is growing like really slowly. In other words: do you have some specific "pattern"?
– GhostCat
Nov 26 '18 at 13:46
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%2f53473789%2fassigning-a-variable-the-identity-of-another%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
1
1) DON'T give ItemNode an amount field. Instead simply have your
getAmount()
method from ItemNode return the value from the item it holds. Nothing more, nothing less. The Decorator design pattern may be what you're looking for.– Hovercraft Full Of Eels
Nov 26 '18 at 1:49
But that won't let me change
ItemNode.amount
andItem.amount
at the same time.– LuminousNutria
Nov 26 '18 at 1:50
1
That's because you shouldn't be changing both, nor should you have both.
– Hovercraft Full Of Eels
Nov 26 '18 at 1:51