I have a java private method and I am trying to find the best way to create an object inside the method. Following are two different approaches that I am trying:
Approach A:
private void createObject() {
Object A;
if (conditionA) {
A = new Object();
//do some Action X with object A
} else {
//Do something without object A
if (conditionB) {
return;
}
A = new Object();
//do some Action Y with object A
}
return;
}
Approach B:
private void createObject() {
Object A = new Object()
if (conditionA) {
//do some action X with Object A
} else {
//Do something without Object A
if (conditionB) {
return;
}
//do some action Y with Object A
}
return;
}
Obviously both the code snippets are working and without any issues. I am just trying to find out the better coding style and standard.