oop - Object Reference passing in Java. Pass by reference working differently in different case? -


while playing in java. saw different behaviour if object modified , given value , different value if assigned new object. here code made show result.

public class test {      int i;      public test(int j) {         this.i = j;     }      public static void main(string[] args) {          test = new test(5);         test n = new test(5);         add(a);         makenew(n);         system.out.println("value of a.i= "+a.i);         system.out.println("value of n.i= "+n.i);      }      private static void add(test t) {         t.i+= 3;         system.out.println("inside method add() t.i= "+t.i);      }      private static void makenew(test t) {          t = new test(8);         system.out.println("inside method makenew() t.i= "+t.i);      }  } 

here output of above code.

inside method add() t.i= 8 inside method makenew() t.i= 8 value of a.i= 8 value of n.i= 5   

in above example object modified value 8. , object b given new object itself. calling them object shows new value. object b shows old value itself. should not showing same value because both case pass refernce? expecting same value a.i , n.i.

here's happens:

test = new test(5); test n = new test(5); 

enter image description here

add(a);  // method add(test t) 

enter image description here

makenew(n)// method makenew(test t) 

enter image description here

t = new test(8); 

enter image description here

system.out.println("value of a.i= "+a.i); system.out.println("value of n.i= "+n.i); 

enter image description here


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -