JAVA passing argument -


i know java passing arguments value (copy). why code returns john ?

public class user {     string name;     public static void main(string[] args) {         user u = new user();         u.name = "sebastian";         system.out.println(u.name);         initialize(u);         system.out.println(u.name);     }     public static void initialize(user u){         u.name = "john";         system.out.println(u.name);         user u2 = new user();         u2.name="dsafsa";         u = u2;         system.out.println(u.name);         u.name = "lilly";         system.out.println(u.name);     } } 

the reference object u being passed initialize method being passed value (the value of reference). doesn't affect how u.name accessed.

when execute u.name = "john" changing u being passed method (via reference). affecting same u in main method.

enter image description here

in image person person user u in main method. inside initialize user u person anotherreferencetothesamepersonobject. updating 1 updates other.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -