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.
in image person person
user u
in main
method. inside initialize
user u
person anotherreferencetothesamepersonobject
. updating 1 updates other.
Comments
Post a Comment