Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lifetime of static (class) variables

for quite some time i was blissfully of the opinion that static [instance] variables exist as long as the app runs. however, to my dismay, and much alarm, i feel that it's not true.

for mere testing, i created a static list of strings, and in my main activity class overrode the onDestroy method to print a message to verify that the app exited. in the onCreate method i simply added a new string, and printed the contents of the list. what i've found is that the size of the list keeps on increasing, and all the string values added previously are still present.

i've read in places [even here on SO] that the instance variables exist as long as the app does, but i fear it's not really so.

to be more precise, i came to be aware of this problem while using the Facebook SDK for Android. i've seen that the AuthListener instances within the list of listeners in the SessionEvents class just keeps on increasing over time. Hence, whenever the app is launched and the user logs in using FB, the listener methods get triggered as many times as there are instances present in the SessionEvents class.

Has someone observed this before, and is there some cardinal mistake i'm committing in understanding how android works?

what gives?

thanks for reading!

[UPDATE]
I stand corrected by BalusC and rdineiu. I really didn't mean to create a confusion here about instance and class variables. Alas, in my haste to post my question i've committed a mistake i didn't wish to. I am very well aware of the difference between static and instance variables. I just intended to write class variables, and can't quite figure out what came over to refer to static variables as instance variables.

However, my question still stands. @MisterSquonk - no, i'm not confusing here about when my Activity ends and when the app gets destroyed. Here's what I tried on a sample - I've got only ONE Activity which serves as the Main. When I press the back button from this Activity, I'm assuming the Activity gets removed from the stack and the app also gets destroyed. I've launched the Task Manager to verify that my app is no longer running.

like image 921
anirvan Avatar asked Dec 16 '22 13:12

anirvan


2 Answers

You seem to not make the distinction between static and instance variables. Static variables are defined on the class itself. Instance variables are present only in class instances.

Example:

class Test {
    public static int a;
}

The variable a is defined on the class itself, not on instances of the class. Each instance will access the same variable. If one instance sets the value of a to 5, every other instance will see the value as 5. The variable will not vanish once the instance vanishes, because it is in no way tied to any instance (it's a class variable). It will continue to be there 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 accessible only from instances of the class. Each instance will have a different copy of the variable. Once the instance gets 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]
  }
}
like image 130
rid Avatar answered Dec 29 '22 00:12

rid


so - i had posted the same question [unfortunately making the same mistake of terming static variables as instance variables] on the Android Developer Google group.

I received some really good responses, especially from Kostya. My interactions on that group helped me to grasp the underlying "rules" of the Android platform.

Hope the message thread helps you as well.

like image 43
anirvan Avatar answered Dec 29 '22 00:12

anirvan