5

I have a class that looks like:

  public class BadCodeStyle {

     private static String theAnswer = null;

     public static void setAnswer(String whatsNew) {
         theAnswer = whatsNew;
     }

     public static String getAnswer() {
          return (theAnswer == null) ? "I don't know" : theAnswer;
     }            

  }

Of course that's a simplification of the actual class. What really happens is that the static method retrieves a framework object if the variable is null. Setting the variable just serves to insert a mock value for test runs where I want to isolate the code from the framework (retrofitting code for testability is fun - like poking your own eye type of fun).

When I do BadCodeStyle.setAnswer("42") the static method behaves like a Singleton (?). I did read the classloader explanation and conclude: the variable will stay as long as the class is loaded and that would be as long as the JVM runs? Is that correct?

Community
  • 1
  • 1
stwissel
  • 20,110
  • 6
  • 54
  • 101

2 Answers2

9

Static class variables live as long as the class definition is loaded. This is usually until the VM exits. However, there are other ways a class can be unloaded. See, for example, this thread and this one.

Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 1
    When is it not until the VM exits? – MikeB May 30 '13 at 02:11
  • 2
    @MikeB - If the class was loaded by a custom class loader and there is no reference from any live code to the custom loader or to anything that it loaded, then the loader and the classes are subject to garbage collection, as described in the two threads I linked to. – Ted Hopp May 30 '13 at 02:16
-1

Static variables are common to all objects (shared) more precisely. it doesn't belong to any instance of class (objects). so its obvious that it cannot be garbage collected with objects.

class X
{
    static string str;
}

X obj1 = new X();
X obj2 = new X();

when you define X.str compiler 'll say replace with Class reference.

But it belongs to Class object. we refer to it as Class variable too. (classloader loads the class) so its single variable (singleton 's actually a pattern that uses single object [use private constructors and using a method to return that single object] )

As you read the memory is reclaimed only when the program is done. it doesn't get (reclaimed) garbage collected in between [Non used objects 'll be garbage collected normally] .

so its lifetime exists as long as the process exists [program is running].

checkout lifetime of variables: www.cs.berkeley.edu/~jrs/4/lec/08

Dineshkumar
  • 4,165
  • 5
  • 29
  • 43
  • 1
    -1: That's not what OP's asking, the question is about how long the `static` variables live in the heap. Check TedHopp's answer. – Luiggi Mendoza May 30 '13 at 02:13
  • After your edit, I would downvote again if I could. The `static` objects will live until the class is still loaded. – Luiggi Mendoza May 30 '13 at 02:16
  • 1
    _"it doesn't belong to any objects"_ - this is wrong. Static variables belong to the class object in which they are declared. If the class itself becomes garbage, then the static variable can disappear from the process heap without the process exiting. – Ted Hopp May 30 '13 at 02:18
  • @TedHopp i meant it doesn't belong to any instance of class. – Dineshkumar May 30 '13 at 02:41
  • 1
    That's still wrong, just not as blatantly wrong. Class objects are still objects--they are instances of `Class`. Also, the info in the lecture notes you linked to is wrong. Under certain circumstances, a class can be unloaded without the program exiting. (However, under _normal_ circumstances, it's usually as you say: static variables usually last until the program exits.) – Ted Hopp May 30 '13 at 02:53