Java stop variable from getting updated of a classMy Face class does not seem to be immutable even though I already declared it as final, how do I correct it?Does a finally block always get executed in Java?Are static class variables possible in Python?Java inner class and static nested classHow do I call one constructor from another in Java?How do I create a Java string from the contents of a file?How to get an enum value from a string value in Java?Get current stack trace in JavaHow to get a JavaScript object's class?Getting the Current Working Directory in JavaStatic Classes In Java
Can a wizard use the spell Levitate on a target and shoot him with attacking spells that don't require concentration?
Need a non-volatile memory IC with near unlimited read/write operations capability
Taking my Ph.D. advisor out for dinner after graduation
What is the meaning of "prairie-dog" in this sentence?
How was the website able to tell my credit card was wrong before it processed it?
Examples of fluid (including air) being used to transmit digital data?
How to say "is going" in Russian in "this game is going to perish"
What's the difference between a type and a kind?
Uniform initialization by tuple
NOLOCK or Read Uncommitted locking / latching behaviours
Tesco's Burger Relish Best Before End date number
What is the shape of the upper boundary of water hitting a screen?
Why SQL does not use the indexed view?
How to understand flavors and when to use combination of them?
Why is there paternal, for fatherly, fraternal, for brotherly, but no similar word for sons?
How do resistors generate different heat if we make the current fixed and changed the voltage and resistance? Notice the flow of charge is constant
In layman's terms, does the Luckstone just give a passive +1 to all d20 rolls and saves except for death saves?
I'm feeling like my character doesn't fit the campaign
Array or vector? Two dimensional array or matrix?
How did the Time Lords put a whole "Star" in a Tardis?
I don't want to be introduced as a "Minority Novelist"
This LM317 diagram doesn't make any sense to me
Was it ever illegal to name a pig "Napoleon" in France?
What is the highest level of accuracy in motion control a Victorian society could achieve?
Java stop variable from getting updated of a class
My Face class does not seem to be immutable even though I already declared it as final, how do I correct it?Does a finally block always get executed in Java?Are static class variables possible in Python?Java inner class and static nested classHow do I call one constructor from another in Java?How do I create a Java string from the contents of a file?How to get an enum value from a string value in Java?Get current stack trace in JavaHow to get a JavaScript object's class?Getting the Current Working Directory in JavaStatic Classes In Java
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am facing a problem as my class variable gets automatically updated with the latest value. But I want separate value for each of the object.
public class B
public String state;
public B()
this.state = "new";
public void firstMethod()
this.state = "first";
public void secondMethod()
this.state = "second";
public abstract class C
public B b;
public C(B service)
this.b = service;
public abstract void execute();
public static void main(String[] args)
B b = new B();
C c = new C(b)
@Override
public void execute()
b.firstMethod();
;
C c1 = new C(b)
@Override
public void execute()
b.secondMethod();
;
c.execute();
c1.execute();
System.out.println(c.b.state);
System.out.println(c1.b.state);
I am getting c and c1 both state equal to "second". I know if I create two separate B object i.e. b1 and b2 and pass those in c and c1 while initializing then it would give me different result as I am expecting. But is there any way to get c.state as "first" and c1.state as "second" with passing the same object to both c and c1?
java class oop
add a comment |
I am facing a problem as my class variable gets automatically updated with the latest value. But I want separate value for each of the object.
public class B
public String state;
public B()
this.state = "new";
public void firstMethod()
this.state = "first";
public void secondMethod()
this.state = "second";
public abstract class C
public B b;
public C(B service)
this.b = service;
public abstract void execute();
public static void main(String[] args)
B b = new B();
C c = new C(b)
@Override
public void execute()
b.firstMethod();
;
C c1 = new C(b)
@Override
public void execute()
b.secondMethod();
;
c.execute();
c1.execute();
System.out.println(c.b.state);
System.out.println(c1.b.state);
I am getting c and c1 both state equal to "second". I know if I create two separate B object i.e. b1 and b2 and pass those in c and c1 while initializing then it would give me different result as I am expecting. But is there any way to get c.state as "first" and c1.state as "second" with passing the same object to both c and c1?
java class oop
But probably it's a duplicate of this: stackoverflow.com/questions/55175471/…
– markspace
Mar 25 at 21:28
where are you callingexecute()
on your objects?
– MFisherKDX
Mar 25 at 21:28
2
Think what you are asking: "I have 3 people. I give person C the home address of person B, and I give person C1 the home address of person B. I then tell person C to go to the home address they have and paint the person living there red. I then tell person C1 to go to the home address they have and paint the person living there blue. After that I check what color person B is, and they are always painted blue. Can I somehow make that person B is both a red painted person and a blue painted person?" No, no you cannot.
– Max Vollmer
Mar 25 at 22:02
I agree. Suppose, I need to inject C and C1 to an array list. Then, is there any way to see the difference between the associated state of C and C1? May be adding some properties in any one of the class or variables that will allow me to store the associated state value with C and C1?
– faysal
Mar 25 at 22:19
add a comment |
I am facing a problem as my class variable gets automatically updated with the latest value. But I want separate value for each of the object.
public class B
public String state;
public B()
this.state = "new";
public void firstMethod()
this.state = "first";
public void secondMethod()
this.state = "second";
public abstract class C
public B b;
public C(B service)
this.b = service;
public abstract void execute();
public static void main(String[] args)
B b = new B();
C c = new C(b)
@Override
public void execute()
b.firstMethod();
;
C c1 = new C(b)
@Override
public void execute()
b.secondMethod();
;
c.execute();
c1.execute();
System.out.println(c.b.state);
System.out.println(c1.b.state);
I am getting c and c1 both state equal to "second". I know if I create two separate B object i.e. b1 and b2 and pass those in c and c1 while initializing then it would give me different result as I am expecting. But is there any way to get c.state as "first" and c1.state as "second" with passing the same object to both c and c1?
java class oop
I am facing a problem as my class variable gets automatically updated with the latest value. But I want separate value for each of the object.
public class B
public String state;
public B()
this.state = "new";
public void firstMethod()
this.state = "first";
public void secondMethod()
this.state = "second";
public abstract class C
public B b;
public C(B service)
this.b = service;
public abstract void execute();
public static void main(String[] args)
B b = new B();
C c = new C(b)
@Override
public void execute()
b.firstMethod();
;
C c1 = new C(b)
@Override
public void execute()
b.secondMethod();
;
c.execute();
c1.execute();
System.out.println(c.b.state);
System.out.println(c1.b.state);
I am getting c and c1 both state equal to "second". I know if I create two separate B object i.e. b1 and b2 and pass those in c and c1 while initializing then it would give me different result as I am expecting. But is there any way to get c.state as "first" and c1.state as "second" with passing the same object to both c and c1?
java class oop
java class oop
edited Mar 25 at 21:51
faysal
asked Mar 25 at 21:26
faysalfaysal
751 silver badge11 bronze badges
751 silver badge11 bronze badges
But probably it's a duplicate of this: stackoverflow.com/questions/55175471/…
– markspace
Mar 25 at 21:28
where are you callingexecute()
on your objects?
– MFisherKDX
Mar 25 at 21:28
2
Think what you are asking: "I have 3 people. I give person C the home address of person B, and I give person C1 the home address of person B. I then tell person C to go to the home address they have and paint the person living there red. I then tell person C1 to go to the home address they have and paint the person living there blue. After that I check what color person B is, and they are always painted blue. Can I somehow make that person B is both a red painted person and a blue painted person?" No, no you cannot.
– Max Vollmer
Mar 25 at 22:02
I agree. Suppose, I need to inject C and C1 to an array list. Then, is there any way to see the difference between the associated state of C and C1? May be adding some properties in any one of the class or variables that will allow me to store the associated state value with C and C1?
– faysal
Mar 25 at 22:19
add a comment |
But probably it's a duplicate of this: stackoverflow.com/questions/55175471/…
– markspace
Mar 25 at 21:28
where are you callingexecute()
on your objects?
– MFisherKDX
Mar 25 at 21:28
2
Think what you are asking: "I have 3 people. I give person C the home address of person B, and I give person C1 the home address of person B. I then tell person C to go to the home address they have and paint the person living there red. I then tell person C1 to go to the home address they have and paint the person living there blue. After that I check what color person B is, and they are always painted blue. Can I somehow make that person B is both a red painted person and a blue painted person?" No, no you cannot.
– Max Vollmer
Mar 25 at 22:02
I agree. Suppose, I need to inject C and C1 to an array list. Then, is there any way to see the difference between the associated state of C and C1? May be adding some properties in any one of the class or variables that will allow me to store the associated state value with C and C1?
– faysal
Mar 25 at 22:19
But probably it's a duplicate of this: stackoverflow.com/questions/55175471/…
– markspace
Mar 25 at 21:28
But probably it's a duplicate of this: stackoverflow.com/questions/55175471/…
– markspace
Mar 25 at 21:28
where are you calling
execute()
on your objects?– MFisherKDX
Mar 25 at 21:28
where are you calling
execute()
on your objects?– MFisherKDX
Mar 25 at 21:28
2
2
Think what you are asking: "I have 3 people. I give person C the home address of person B, and I give person C1 the home address of person B. I then tell person C to go to the home address they have and paint the person living there red. I then tell person C1 to go to the home address they have and paint the person living there blue. After that I check what color person B is, and they are always painted blue. Can I somehow make that person B is both a red painted person and a blue painted person?" No, no you cannot.
– Max Vollmer
Mar 25 at 22:02
Think what you are asking: "I have 3 people. I give person C the home address of person B, and I give person C1 the home address of person B. I then tell person C to go to the home address they have and paint the person living there red. I then tell person C1 to go to the home address they have and paint the person living there blue. After that I check what color person B is, and they are always painted blue. Can I somehow make that person B is both a red painted person and a blue painted person?" No, no you cannot.
– Max Vollmer
Mar 25 at 22:02
I agree. Suppose, I need to inject C and C1 to an array list. Then, is there any way to see the difference between the associated state of C and C1? May be adding some properties in any one of the class or variables that will allow me to store the associated state value with C and C1?
– faysal
Mar 25 at 22:19
I agree. Suppose, I need to inject C and C1 to an array list. Then, is there any way to see the difference between the associated state of C and C1? May be adding some properties in any one of the class or variables that will allow me to store the associated state value with C and C1?
– faysal
Mar 25 at 22:19
add a comment |
2 Answers
2
active
oldest
votes
Your c
and c1
share the exact same B
object, namely b
.
Since the state
we are discussing belongs to b
, there is only one of them - called b.state
.
If you want state
to be different, you cannot use the same B
object with both C
objects.
This likely boils down to a misunderstanding of the reference semantics of assignment and parameter passing. Neither assignment nor parameter-passing makes a copy of an object.
But is there any way to get c.state as "first" and c1.state as
"second" with passing the same object to both c and c1?
This could not possibly work. Suppose a single B
object could somehow have any number of values of state
. Whenever you wrote b.state
, how would the system know which one you meant?
Well, that's not entirely true as I wrote it. If the constructor of C
only used its B
object to make an entirely new copy, such that it kept the copy rather than the original, then you'd achieve the effect; there would be three B
objects in the world, the original, the copy made by c
, and the copy made by c1
. But that has no advantage here over just passing in separate B
instances to begin with.
Yes I got your point. Actually I was thinking whether it will be a good idea to create a new object or there is another way of dealing this situation. So I think I need to create two separte b object when create c and c1.
– faysal
Mar 25 at 22:08
"separate B" seems the simple approach, unless this is just an illustration of some actual situation that would make it more complicated.
– another-dave
Mar 25 at 22:10
Actually in my context, I am highly restricted to create a separate B. Can you tell me if there is another class A. And B extends A. in that case, how would I tackle this problem with copy constrcutor or may adopt any other approach?
– faysal
Mar 25 at 22:16
I have solved the problem by cloning the object.
– faysal
Mar 25 at 22:53
add a comment |
You used the same instance of B class into your 2 objects, so your state variable will always get updated by the last called method, you need to use another instance of B to have two separated state property.
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%2f55346636%2fjava-stop-variable-from-getting-updated-of-a-class%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
Your c
and c1
share the exact same B
object, namely b
.
Since the state
we are discussing belongs to b
, there is only one of them - called b.state
.
If you want state
to be different, you cannot use the same B
object with both C
objects.
This likely boils down to a misunderstanding of the reference semantics of assignment and parameter passing. Neither assignment nor parameter-passing makes a copy of an object.
But is there any way to get c.state as "first" and c1.state as
"second" with passing the same object to both c and c1?
This could not possibly work. Suppose a single B
object could somehow have any number of values of state
. Whenever you wrote b.state
, how would the system know which one you meant?
Well, that's not entirely true as I wrote it. If the constructor of C
only used its B
object to make an entirely new copy, such that it kept the copy rather than the original, then you'd achieve the effect; there would be three B
objects in the world, the original, the copy made by c
, and the copy made by c1
. But that has no advantage here over just passing in separate B
instances to begin with.
Yes I got your point. Actually I was thinking whether it will be a good idea to create a new object or there is another way of dealing this situation. So I think I need to create two separte b object when create c and c1.
– faysal
Mar 25 at 22:08
"separate B" seems the simple approach, unless this is just an illustration of some actual situation that would make it more complicated.
– another-dave
Mar 25 at 22:10
Actually in my context, I am highly restricted to create a separate B. Can you tell me if there is another class A. And B extends A. in that case, how would I tackle this problem with copy constrcutor or may adopt any other approach?
– faysal
Mar 25 at 22:16
I have solved the problem by cloning the object.
– faysal
Mar 25 at 22:53
add a comment |
Your c
and c1
share the exact same B
object, namely b
.
Since the state
we are discussing belongs to b
, there is only one of them - called b.state
.
If you want state
to be different, you cannot use the same B
object with both C
objects.
This likely boils down to a misunderstanding of the reference semantics of assignment and parameter passing. Neither assignment nor parameter-passing makes a copy of an object.
But is there any way to get c.state as "first" and c1.state as
"second" with passing the same object to both c and c1?
This could not possibly work. Suppose a single B
object could somehow have any number of values of state
. Whenever you wrote b.state
, how would the system know which one you meant?
Well, that's not entirely true as I wrote it. If the constructor of C
only used its B
object to make an entirely new copy, such that it kept the copy rather than the original, then you'd achieve the effect; there would be three B
objects in the world, the original, the copy made by c
, and the copy made by c1
. But that has no advantage here over just passing in separate B
instances to begin with.
Yes I got your point. Actually I was thinking whether it will be a good idea to create a new object or there is another way of dealing this situation. So I think I need to create two separte b object when create c and c1.
– faysal
Mar 25 at 22:08
"separate B" seems the simple approach, unless this is just an illustration of some actual situation that would make it more complicated.
– another-dave
Mar 25 at 22:10
Actually in my context, I am highly restricted to create a separate B. Can you tell me if there is another class A. And B extends A. in that case, how would I tackle this problem with copy constrcutor or may adopt any other approach?
– faysal
Mar 25 at 22:16
I have solved the problem by cloning the object.
– faysal
Mar 25 at 22:53
add a comment |
Your c
and c1
share the exact same B
object, namely b
.
Since the state
we are discussing belongs to b
, there is only one of them - called b.state
.
If you want state
to be different, you cannot use the same B
object with both C
objects.
This likely boils down to a misunderstanding of the reference semantics of assignment and parameter passing. Neither assignment nor parameter-passing makes a copy of an object.
But is there any way to get c.state as "first" and c1.state as
"second" with passing the same object to both c and c1?
This could not possibly work. Suppose a single B
object could somehow have any number of values of state
. Whenever you wrote b.state
, how would the system know which one you meant?
Well, that's not entirely true as I wrote it. If the constructor of C
only used its B
object to make an entirely new copy, such that it kept the copy rather than the original, then you'd achieve the effect; there would be three B
objects in the world, the original, the copy made by c
, and the copy made by c1
. But that has no advantage here over just passing in separate B
instances to begin with.
Your c
and c1
share the exact same B
object, namely b
.
Since the state
we are discussing belongs to b
, there is only one of them - called b.state
.
If you want state
to be different, you cannot use the same B
object with both C
objects.
This likely boils down to a misunderstanding of the reference semantics of assignment and parameter passing. Neither assignment nor parameter-passing makes a copy of an object.
But is there any way to get c.state as "first" and c1.state as
"second" with passing the same object to both c and c1?
This could not possibly work. Suppose a single B
object could somehow have any number of values of state
. Whenever you wrote b.state
, how would the system know which one you meant?
Well, that's not entirely true as I wrote it. If the constructor of C
only used its B
object to make an entirely new copy, such that it kept the copy rather than the original, then you'd achieve the effect; there would be three B
objects in the world, the original, the copy made by c
, and the copy made by c1
. But that has no advantage here over just passing in separate B
instances to begin with.
edited Mar 25 at 22:07
answered Mar 25 at 21:59
another-daveanother-dave
1,6632 silver badges10 bronze badges
1,6632 silver badges10 bronze badges
Yes I got your point. Actually I was thinking whether it will be a good idea to create a new object or there is another way of dealing this situation. So I think I need to create two separte b object when create c and c1.
– faysal
Mar 25 at 22:08
"separate B" seems the simple approach, unless this is just an illustration of some actual situation that would make it more complicated.
– another-dave
Mar 25 at 22:10
Actually in my context, I am highly restricted to create a separate B. Can you tell me if there is another class A. And B extends A. in that case, how would I tackle this problem with copy constrcutor or may adopt any other approach?
– faysal
Mar 25 at 22:16
I have solved the problem by cloning the object.
– faysal
Mar 25 at 22:53
add a comment |
Yes I got your point. Actually I was thinking whether it will be a good idea to create a new object or there is another way of dealing this situation. So I think I need to create two separte b object when create c and c1.
– faysal
Mar 25 at 22:08
"separate B" seems the simple approach, unless this is just an illustration of some actual situation that would make it more complicated.
– another-dave
Mar 25 at 22:10
Actually in my context, I am highly restricted to create a separate B. Can you tell me if there is another class A. And B extends A. in that case, how would I tackle this problem with copy constrcutor or may adopt any other approach?
– faysal
Mar 25 at 22:16
I have solved the problem by cloning the object.
– faysal
Mar 25 at 22:53
Yes I got your point. Actually I was thinking whether it will be a good idea to create a new object or there is another way of dealing this situation. So I think I need to create two separte b object when create c and c1.
– faysal
Mar 25 at 22:08
Yes I got your point. Actually I was thinking whether it will be a good idea to create a new object or there is another way of dealing this situation. So I think I need to create two separte b object when create c and c1.
– faysal
Mar 25 at 22:08
"separate B" seems the simple approach, unless this is just an illustration of some actual situation that would make it more complicated.
– another-dave
Mar 25 at 22:10
"separate B" seems the simple approach, unless this is just an illustration of some actual situation that would make it more complicated.
– another-dave
Mar 25 at 22:10
Actually in my context, I am highly restricted to create a separate B. Can you tell me if there is another class A. And B extends A. in that case, how would I tackle this problem with copy constrcutor or may adopt any other approach?
– faysal
Mar 25 at 22:16
Actually in my context, I am highly restricted to create a separate B. Can you tell me if there is another class A. And B extends A. in that case, how would I tackle this problem with copy constrcutor or may adopt any other approach?
– faysal
Mar 25 at 22:16
I have solved the problem by cloning the object.
– faysal
Mar 25 at 22:53
I have solved the problem by cloning the object.
– faysal
Mar 25 at 22:53
add a comment |
You used the same instance of B class into your 2 objects, so your state variable will always get updated by the last called method, you need to use another instance of B to have two separated state property.
add a comment |
You used the same instance of B class into your 2 objects, so your state variable will always get updated by the last called method, you need to use another instance of B to have two separated state property.
add a comment |
You used the same instance of B class into your 2 objects, so your state variable will always get updated by the last called method, you need to use another instance of B to have two separated state property.
You used the same instance of B class into your 2 objects, so your state variable will always get updated by the last called method, you need to use another instance of B to have two separated state property.
answered Mar 25 at 22:07
AYOUB_MAAYOUB_MA
385 bronze badges
385 bronze badges
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%2f55346636%2fjava-stop-variable-from-getting-updated-of-a-class%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
But probably it's a duplicate of this: stackoverflow.com/questions/55175471/…
– markspace
Mar 25 at 21:28
where are you calling
execute()
on your objects?– MFisherKDX
Mar 25 at 21:28
2
Think what you are asking: "I have 3 people. I give person C the home address of person B, and I give person C1 the home address of person B. I then tell person C to go to the home address they have and paint the person living there red. I then tell person C1 to go to the home address they have and paint the person living there blue. After that I check what color person B is, and they are always painted blue. Can I somehow make that person B is both a red painted person and a blue painted person?" No, no you cannot.
– Max Vollmer
Mar 25 at 22:02
I agree. Suppose, I need to inject C and C1 to an array list. Then, is there any way to see the difference between the associated state of C and C1? May be adding some properties in any one of the class or variables that will allow me to store the associated state value with C and C1?
– faysal
Mar 25 at 22:19