Android garbage collector - when do static (class level) var get reset?

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?

Answer:1

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.

Answer:2

I am a SolrJ beginner and want to find out whats the fastest way to determine if an document with an unique ID exists? I don't need the document, I just want to find out if it is already in the index....

I have a Validator interface which provides a isValid(Thing) method, returning a ValidationResult which contains a boolean and a reason message. I want to create a ValidatorAggregator implementation ...

I'm stuck. Coming from C++ I thought this would simply work, but it does not. Can you please give me an advice? I will try to not end up with kind of creation method in each class used for T. public ...

Is there a way to set a button key event in Java so that Alt does not have to be pressed. For example, when this is used setMnemonic(KeyEvent.VK_DELETE) it is required for Alt + Delete to be pressed ...