I have a method where it takes in an object as an input
void doSomething(MyObject myObj) {
///
}
In this method, it calls myObj.getValue() several times for various reasons
Is it better to just store as int objValue = myObj.getValue() and use objValue throughout the method, instead keep calling myObj.getValue()
why? why not?
getValuedoes. If it accesses a final field or other clearly fixed property the JIT compiler will optimise that away most of the time. If it actually runs some computation then it will rerun the computation each time.int objValue = myObj.getValue()myObj.getValue()could be changed by other threads.