3

How does Android handle static classes? In particular, if I declare a static variable like this:

private static boolean someBoolean = true;
...
// Somewhere else in the code I do this:
    someBoolean = false;

Let's also say that that last line is the only time someBoolean's value changes from its initialized value. How long will someBoolean stay false? How can the user reset this? Will force closing the app work? Do you have to uninstall the app? Clear its data? Its cache?

What if this static variable is in someone else's SDK? I think I understand how variables are re-instantiated when they're in the app code that I wrote, but what if this is code that's loaded from some jar -- when will someBoolean get re-declared and subsequently initialized to true? Similar to above, how can the user force this behavior? Force close? Clear data?

boo-urns
  • 10,136
  • 26
  • 71
  • 107

2 Answers2

4

Static variables are initialized when class is loaded by the ClassLoader. Every Virtual Machine instance will have at lease one ClassLoader. Every java process be it on any OS will have one Virtual Machine. Thus to reset the variable you will have to force kill/stop the process. Remember in Android a process associated with an Activity will continue to remain in background and hence will retain all its static variables even after Activity is pause.

You can verify this behaviour by using DDMS and force killing the process associated with your Activity.

sharadendu sinha
  • 827
  • 4
  • 10
  • ... but, in background, eventually the process gets killed. – Michael Sep 18 '12 at 18:36
  • 1
    Yes, but you cannot count on it. You shouldn't base your app on the assumption that static variables will always keep their value even if the app is in background. – Michael Sep 18 '12 at 18:39
  • correct its never a good practice to rely on it. which is why Android provides several Life cycle methods where by you can persist application state ... – sharadendu sinha Sep 18 '12 at 18:41
  • To confirm, if an activity is in the foreground, any static variables will never be garbage collected right? – LuckyMe Jul 10 '13 at 11:13
2

When class is unloaded, then static variable someBoolean will be eligible for GC.

someBoolean will be initiated on class initiation (after load).

someBoolean stay false until you set another value in code.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • When does the class get unloaded? – boo-urns Sep 18 '12 at 18:27
  • Please read this thread http://stackoverflow.com/questions/6710328/when-is-a-class-is-unloaded – kosa Sep 18 '12 at 18:27
  • Ah, so this is Java-specific and not at all tied to the Android application life cycle? – boo-urns Sep 18 '12 at 18:28
  • Android Dalvik VM built mainly like JVM with some exceptions. But, yes Dalvik VM also follows similar functionality. So, they are together. – kosa Sep 18 '12 at 18:29
  • @DarrenGreen Android is (from a programmers view) running standard Java 5/6 so Android can't change a fundamental behavior like statics. It's just the app start / stop that is managed by the system. Not the explicit way you start stop apps on a desktop (and hence no exit button) – zapl Sep 18 '12 at 18:50
  • But you should keep in mind that every app lives in its own vm, so if the app gets killed the vm gets killed also. – Michael Sep 18 '12 at 19:27