0

So my question is if an object A has an attribute such as int var; and object B has an attribute int var2 both of the object are instantiated from the same class using the same constructor. For illustrations purposes lets say that var=3 and var2=0 Does assigning object A=B make A.var==0 ? please do motivate your answer for me.

here is more code in case you need more:

public class CarSize{ public CarSize(int x) { this.var=x; } public void call() { doubleVar(this); } private void doubleVar(CarSize cz) { int vara= cz.getVar()*2; CarSize c= new CarSize(vara); cz=c; } public int getVar(){return this.var;} private int var; public static void main(String args[]) { CarSize cz = new CarSize(3); cz.call(); System.out.println(cz.getVar()); } } 

ANNOUNCEMENT

people please check the code before attemtpting to explain to me what i already know, thanks a lot for your understanding.

5
  • Well, this is not changing the field in A, assignment just makes A point to same place as B, so yes, this field will become the same as in B Commented Jul 14, 2017 at 18:50
  • but it doesnt, at least the code that i showed says otherwise, i mean it came as a shock to me Commented Jul 14, 2017 at 18:53
  • And what are you expecting? That your code will return 6? Can't fully get the logic. And I would also suggest cz = new CarSize(vara); without a temporary object Commented Jul 14, 2017 at 19:00
  • @Semant1ka yes i was expecting 6, thank god someone actually read my code before typing a comment on it. what is wrong with my logic there, i mean if A.var= 1 and B.var =2 and i say A=Bshouldnt A.varbe =2 since A is now pointing to the same address as B and old values of A will get garbage collected? Commented Jul 14, 2017 at 19:04
  • in seems that @Sweeper gives you a good point on why it is not working Commented Jul 14, 2017 at 19:06

4 Answers 4

2

The problem why your code does not work as expected lies at this short one of code:

cz=c; 

Since CarSize is a reference type, variables of CarSize store "pointers" to CarSize objects, not the CarSize objects themselves.

When you are passing a car size as a parameter, you are passing a "pointer". cz=c makes the pointer stored in cz point to the same object as that stored in c.

Now let's see how you call this method:

doubleVar(this); 

this refers to the cz that is declared in the main method. Again, this points to a car size object. In the doubleVar method, you made the parameter point to another object, but you only made the parameter point to something else. this is never changed, neither did its fields.

That's why you don't see the values doubling.

To fix this, don't assign the parameter a new value. Instead, change this directly. Get rid of the parameter because that is not needed.

private void doubleVar() { this.var *= 2; // you can actually omit "this.", I put it there just to make it clear for you } 
Sign up to request clarification or add additional context in comments.

Comments

0

You can assign a reference to an object to another object, you cannot assign an object to another object. In your case both A and B would reference the same object previously referenced by only B. Object referenced previously by A will be taken by the garbage collector and cease to exist.

Comments

0

In Java, using the = operator with non-primitives results in aliasing. If you have two different objects of the same class, a and b, saying a = b; makes a the same object as b, and garbage collection will eventually destroy what used to be in a.

So, yes, the field will be the same, but changing a's value of the field will also change b's value of the field because a and b are now the same.

Comments

0
public class Test{ public static void main(String[] args){ object a = new object(3); object b = new object(0); a = b; System.out.println(a.var); } } public class object{ protected int var; public object(int x){ var = x; } } 

This is code of the situation you described exactly. The output here would be "0" because when a programmer defined object is set equal to another object of the same type it overwrites the reference from the original value of 3 to the new value of 0

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.