Can someone explain to me why this gives me a "local variable - redundant error"?
Because you can trivially write this without using a local variable.
public double depreciationAmount() { return cost * percentDepreciated; }
Therefore, the local variable is not needed / redundant.
However, I assume this is NOT a compiler error. This may be a compiler warning, or most likely a style check or an error warning. This is something you could ignore without risking the correctness of your code ... as it is written.
In addition, I would predict that after the code was compiled by JIT (using the modern compiler Hotspot JIT ...), there will be no difference in performance between the two versions.
source share