Open dcow opened 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.
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.
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
Variables should be non-static by default and only promoted (per-say) to static when needed. Most uses in this code base are misinformed.