I’ve noticed that the contents of an object variable can be altered when the variable is passed through a void method, while the value of a primitive variable cannot be.
For example, the following logs 'old string'
public void StringTest(String s){
s = 'new string';
}
String str = 'old string';
StringTest(str);
System.debug(str); // logs 'old string'
... while the following logs 'new string'
public class SomeClass{
public String st;
}
public void ClassTest(SomeClass c){
c.st = 'new string';
}
SomeClass sc = new SomeClass();
sc.st = 'old string';
ClassTest(sc);
System.debug(sc.st); // logs 'new string'
Why does it work this way? Apologies if this is a super basic concept. I am self-taught and therefore know nothing.