How does this fit in the "pass by value of refrences" in java? -


this question has answer here:

in code below why foo2 null when printing data @ system.out.print?

public class helper {     public void shadowcopy(foo foo1, foo foo2){         foo2 = foo1;     }      public static void main(string[] args) {         helper h = new helper();         foo foo1 = new foo(50);         foo foo2= null;         h.shadowcopy(foo1, foo2);      system.out.println(foo2.data);// why  java.lang.nullpointerexception?     }      public static class foo {          public int data=0;         public foo(int data){             this.data = data;         }     }  } 

in shadowcopy, foo2 copied reference same object foo2 in main referring to. however, assigns local foo2 reference refer same object foo1. doesn't change foo2 reference variable in main, remains null. leads npe.

to desired behavior, place foo2 = foo1; in main, you're not dealing copies of reference variables.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -