The lifetime of static (class) variables

for some time, I was blissfully aware that static variables [instance] exist while the application was running. however, to my horror and dismay, I feel that this is not true.

for simple testing, I created a static list of strings, and in my main class activity override the onDestroy method to print a message to verify that the application exited. in the onCreate method onCreate I just added a new string and printed the contents of the list. I found that the size of the list continues to increase, and all the previously added row values ​​are still present.

I read in places [even here on SO] that instance variables exist as long as the application is running, but I'm afraid it is not.

To be more precise, I found out about this issue when using the Android SDK for Android. I saw that the AuthListener instances in the list of listeners in the SessionEvents class simply continue to grow over time. Therefore, whenever the application starts and the user logs in using FB, the listener methods are run as many times as there are instances present in the SessionEvents class.

Someone has watched this before, and is there some kind of fundamental error that I make in understanding how the android works?

what gives?

thank you for reading!

[ UPDATE ]
I am fixing BalusC and rdineiu . I really did not want to create confusion here about instance and class variables. Alas, in my haste to publish my question, I made a mistake that I did not want. I know very well the difference between static and instance variables. I just intended to write class variables and can't figure out what happened to refer to static variables as instance variables.

However, my question is still standing. @MisterSquonk - no, I'm not confused here when my activity ends and when the application is destroyed. Here is what I tried on the sample - I have only ONE activity that serves as Main . When I click the back button from this activity , I assume that the activity is removed from the stack and the application is also destroyed. I started the task manager to verify that my application is no longer working.

+4
source share
3 answers

so I wrote the same question [unfortunately, making the same mistake as static variables as instance variables]) in the Android Developer team.

I got some really good reviews, especially from Kostya . My interactions in this group helped me understand the basic "rules" of the Android platform.

Hope the message flow helps you.

+5
source

You don't seem to distinguish between static and instance variables. Static variables are defined by the class itself. Instance variables are only present in class instances.

Example:

 class Test { public static int a; } 

The variable a determined by the class itself, not by instances of the class. Each instance will access the same variable. If one instance sets the value of a to 5 , each other instance will see the value as 5 . The variable will not disappear as soon as the instance disappears, because it is in no way tied to any instance (this is a class variable). It will continue to exist until the end of time (or until the application exits, whichever comes first).

On the other hand, the following example uses an instance variable:

 class Test { public int a; } 

This variable will be available only from instances of the class. Each instance will have a different copy of the variable. Once the instance is destroyed, the variable goes with it.


To illustrate:

 import java.util.List; import java.util.ArrayList; class Test { // instanceVar will be initialized whenever a new Test object is created private List<Integer> instanceVar = new ArrayList<Integer>(); // staticVar will be initialized right now private static List<Integer> staticVar = new ArrayList<Integer>(); public void updateInstanceVar() { instanceVar.add(1); instanceVar.add(2); } public void updateStaticVar() { staticVar.add(1); staticVar.add(2); } public static void main(String[] args) { Test test1 = new Test(); test1.updateInstanceVar(); // test1.instanceVar = [1, 2] test1.updateStaticVar(); // Test.staticVar = [1, 2] Test test2 = new Test(); test2.updateInstanceVar(); // test2.instanceVar = [1, 2] test2.updateStaticVar(); // Test.staticVar = [1, 2, 1, 2] } } 
+6
source

Instance variables are destroyed when the application does, but some gui widgets retain their state by default and then restore it to the onCreate method.

0
source

All Articles