I am working on an android launcher based on the stock launcher. I am just interested why are there lots of global variables converted to local variables in methods e.g.
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000);
instead of just
mVelocityTracker.computeCurrentVelocity(1000);
Is it some android thing or a general java rule? It makes no sense allocating a new VelocityTracker when it can be accessed directly.
EDIT
Yes this code is being repeated many times.
This can be useful if you are using a field many times. Some JVM and I assume Android VMs don’t optimise access to fields as efficiently.
However it can be overused and I don’t see the point if only accessed once.
It can also be useful of you are accessing a volatile field. This ensures when you use that field many times you are taking about the same object. e.g.
If you didn’t use a local variable
textcould be non-null for the if statement andnullfor the doSomething().