java - Can't decrease value of int variable -
i created 4 classes, mainp
class, , others ex2
, ex3
, ex4
(3 , 4 extend 2). when call object whice decreases int
variable of object, on first call.
these classes:
ex2:
protected int par; public int getpar() { return par; } public void setpar(int par) { this.par = par; }
ex3:
private int par; public ex3(int par) { this.par = par; } public void bar(ex2 b) { b.setpar(par - 10); } public int getpar() { return par; } public void setpar(int par) { this.par = par; }
ex4 same ex3.
main:
public class mainp { public static void main(string[] args) { ex3 kaka = new ex3(100); ex4 tana = new ex4(100); kaka.bar(tana); system.out.println(tana.getpar()); kaka.bar(tana); system.out.println(tana.getpar()); } }
output:
90
90
i don't understand why par
decreases in first call , stops @ second call. suppose it's calibrated somehow, don't see why.
edit: understood silly mistake, have new 1 though. changed method to:
public void bar(ex2 b){ b.setpar(b.par-=10); }
and output is:
-10
-20
why method changes b.par -10? if this: tana.setpar(tana.par-=10); @ main method, , changing variables public, not change value of tana -10, , decreases 100.
public void bar(ex2 b){ b.setpar(par - 10); }
here set b
value of par - 10
par
not field of b
. , because hasn't changed 2 times set b
par
field same value - 90.
Comments
Post a Comment