0

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;
}
Janardhan
  • 13
  • 6
  • Except for the fact that `propertyBean` goes out of scope after `parentMethod`, that's fine... – Makoto Oct 27 '15 at 15:53
  • Thanks for the reply. I need some suggestion on this. Here the code in childMethod() will be tightly coupled and couldn't be used for unit testing easily. So do you have any suggestions to improve the code here? – Janardhan Oct 28 '15 at 06:50

1 Answers1

0

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)

AbtPst
  • 7,778
  • 17
  • 91
  • 172
  • Thanks for the reply. I need some suggestion on this. Here the code in childMethod() will be tightly coupled and couldn't be used for unit testing easily. So do you have any suggestions to improve the code here? – Janardhan Oct 28 '15 at 06:44
  • your code seems fine. like i said, you dont need to return the object. also if all you need to do is set the value of value2, which i assume is a member of the PropertyBean class, thenjust use the setter method for that member. are you familiar with the concept of getters and setters? – AbtPst Oct 28 '15 at 12:20