Confusing Java parameter manipulation -
when i'm using a=x, b=y result 15. when i'm using x=a,y=b, result 0. please explain why?
public class test { int , b; test(int x, int y) { a=x; b=y; } int sr() { return a*b; } public static void main (string args[]){ test t=new test(5,3); system.out.println(t.sr()); } }
a=x; b=y;
will set instance attributes a
& b
values passed values x
, y
. when create instance using :
test t=new test(5,3);
a , b have values 5 & 3 respectively. hence calling method t.sr
return
a*b = 5*3 = 15;
on other hand if u use:
x = a; y = b;
local variables x , y set default values of , b i.e. 0. , b hold defualt values 0 no other value set. when u
test t=new test(5,3);
as & b have values 0 calling method t.sr
return
a*b = 0*0 = 0;
Comments
Post a Comment