give-me-coinsMonitoringApp icon indicating copy to clipboard operation
give-me-coinsMonitoringApp copied to clipboard

Remove unnecessary uses of static variables.

Open dcow opened this issue 11 years ago • 3 comments

Variables should be non-static by default and only promoted (per-say) to static when needed. Most uses in this code base are misinformed.

dcow avatar Dec 07 '13 14:12 dcow

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.

Letme avatar Dec 07 '13 17:12 Letme

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:

  1. Long held static references to Activity and Context can cause memory issues.
  2. Potential for NullPointerException because under memory pressure the class (and thus the static fields) can be unloaded by the ClassLoader or the VM may be killed all together. In either case, the OS does not treat static fields like non-static fields; the static fields are not reloaded when the app comes back into the foreground.

justinmuller avatar Dec 07 '13 17:12 justinmuller

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

  1. I'm mostly talking about the main activity. I only quick oy browsed everything else. And
  2. that there are certainly valid uses of static in Android. (This is not a "statics are evil" rant (= )

dcow avatar Dec 07 '13 20:12 dcow