1

Why isn't a NullPointerException thrown if the instance of the Statics class is set to null? I understand that a static variable is shared amongst all instances and their is only 1 copy. Does this mean that static variables don't live on the heap? Well I guess that's what it implies?

So I guess the bigger question is where do static variables/methods live?

Consider the following:

public class Statics {

public static int count = 0;

public static void main(String[] args){
    System.out.println(Statics.count);
}
}


public class StaticsTest {
public static void main(String[] args){
    Statics t1 = new Statics();
    t1 = null;
    System.out.println(t1.count); //Output of zero was printed which I didn't expect.
}
}

If they don't live on the heap, I'm guessing they don't get garbage collected?

trincot
  • 317,000
  • 35
  • 244
  • 286
RamanSB
  • 1,162
  • 9
  • 26

1 Answers1

1

There is no point of heap, memory, GC at all ..

The point is static members belongs to class rather than instance. So Java just need to know the it's type though the instance is null

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307