Can someone please explain the output of the below code. Why is line 5 not printing null but line 8 printing the updated values from changeValue method. I saw many posts about pass by value but they don't give details about the assigning the "null" reference.
public class ObjectPassbyvalueTest {
public static void main(String[] args) {
Student s = new Student("A");
change(s);
System.out.println(s); // Why is this printing the Student Object and
// not null?
changeValue(s);
System.out.println(s); // This is printing the updated Student Object
// with name "B"
}
public static void change(Student s) {
s = null;
}
public static void changeValue(Student s) {
s.name = "B";
}
}
class Student {
String name;
public Student(String name) {
super();
this.name = name;
}
@Override
public String toString() {
return "Student [name=" + name + "]";
}
}
Output: Student [name=A] Student [name=B]