Search code examples
javacode-standards

Passing an object between the methods which has to keep adding data in all the methods


I have a requirement of passing the object between methods and adding data as below example. Is the below written code is a good programming practice?

public void parentMethod(){
PropertyBean propertyBean = new PropertyBean();
propertyBean.SetValue1("Value1");
propertyBean = childMethod(propertyBean);
propertyBean.SetValue3("Value3");
}
public PropertyBean childMethod(PropertyBean propertyBean){
propertyBean.SetValue2("Value2");
return propertyBean;
}

Solution

  • you dont need to return the object

    childMethod(propertyBean);
    

    this call would make sure that Value2 is set for this object. Most Objects are passed by reference in Java (except for primitive types)