give-me-coinsMonitoringApp
give-me-coinsMonitoringApp copied to clipboard
Remove unnecessary uses of static variables.
Variables should be non-static by default and only promoted (per-say) to static when needed. Most uses in this code base are misinformed.
As far as I understand, global variables in java should be declared static. I think I've only added static where compiler pointed it out, but I could have been wrong.
I had just started a local branch yesterday were I started to remove static
variables and will continue to do so. I agree with @dcow in general and suggest that this is even more serious in Android because:
- Long held
static
references toActivity
andContext
can cause memory issues. - Potential for
NullPointerException
because under memory pressure the class (and thus thestatic
fields) can be unloaded by the ClassLoader or the VM may be killed all together. In either case, the OS does not treatstatic
fields like non-static fields; thestatic
fields are not reloaded when the app comes back into the foreground.
As @justinmuller outlines, there are Android-specific pitfalls to avoid when using static variables.
Further, your application instance is guaranteed to be a singleton so global variables that need not be persistent should live there. The use of static
in this code base seems to be something between attempting to have global variables and attempting to have static singletons.
Since static can result in misleading behavior especially on Android, and because the application class is the proper location for non-persistent shared references, I used misinformed to describe the uses I saw.
I should add
- I'm mostly talking about the main activity. I only quick oy browsed everything else. And
- that there are certainly valid uses of static in Android. (This is not a "statics are evil" rant (= )