I could find a lot of information about pass-by-reference and pass-by-value about java method arguments but not of the following kind where a method1 argument is modified in another method2 and simultaneously set to itself in method1.
public class TestQDoc {
public QDoc method1(QDoc doc) {
doc = (QDoc) method2(doc);
return doc;
}
public QDoc method2(QDoc doc) {
doc.x = 100;
return doc;
}
public static void main(String [] args) {
QDoc doc = new QDoc();
doc.x=200;
TestQDoc tq = new TestQDoc();
doc=tq.method1(doc);
System.out.println(doc.x);
}
static class QDoc {
public Integer x;
}
}
The above code is printing 100 as expected. I am not sure if the method arguments are passed by reference or passed by value. Also, is there any possibility that method1 fails to return the modified doc in method2?