Letme / give-me-coinsMonitoringApp

Android app for monitoring workers on give-me-coins.com mining pool.
GNU General Public License v3.0
24 stars 21 forks source link

Remove unnecessary uses of static variables. #20

Open dcow opened 10 years ago

dcow commented 10 years ago

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

Letme commented 10 years ago

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.

justinmuller commented 10 years ago

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.

dcow commented 10 years ago

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 (= )