+91 81900 98289

Java Objects

Each new call creates another object. Changing the name on one student does not change the other. The variable holds a reference. Copying the variable copies the reference, not a second student.

Passing an object to a method passes the reference. The method can change the object's fields. Reassigning the parameter to a new object does not change the caller's variable.

Example

b is not a second copy of the text. It is a second name for the same builder.

public class Pair { public static void main(String[] args) { StringBuilder a = new StringBuilder("Java"); StringBuilder b = a; b.append(" 17"); System.out.println(a); } }
String cannot be appended in place. StringBuilder can. a and b point at one builder, so append shows up through both names.