2

I apologize if this has been asked before.

I have the following scenario:

public class Distress{
    private static Distress distressIns;

    private MyFirstClass aClass;
    private MySecondClass bClass;

    private Distress(){
       aClass = new MyFirstClass();
       bClass = new MySecondClass();
    }

    ///update: this is a singleton class
    public static getIns(){
        if (distressIns == null){
            distressIns = new Distress();        
         }
        return distressIns;
    }

    public static destroyIns(){
        distressIns = null;
    }
}

In the above scenario if someone calls destroyIns(), would aClass and bClass also become null?

Sunny
  • 7,444
  • 22
  • 63
  • 104
  • 3
    Why don't you test it? `Distress d = Distress.getIns(); Distress.destroyIns(); System.out.println(d.getAClass());` – JB Nizet Sep 14 '14 at 18:46
  • related: http://stackoverflow.com/questions/3299604/interview-question-objects-eligible-for-garbage-collection?rq=1 – William Price Sep 14 '14 at 18:50
  • Here a good explanation of the problem: http://stackoverflow.com/questions/16599137/does-java-gc-destroy-objects-if-instance-variables-still-have-reference – conFusl Sep 14 '14 at 18:50
  • 1
    `new Distress;` -> `new Distress();` I haven't got enough rep to edit myself. – bcsb1001 Sep 14 '14 at 18:57

1 Answers1

4

No. First, someone could be holding on to a reference to distressIns (your getIns() method is public after all, so there could be another reference to it).

Second, variables do not "become null" just because they are garbage collected. The objects may or may not be garbage collected, again depending on whether another reference exists.

markspace
  • 10,621
  • 3
  • 25
  • 39
  • If someone is holding a reference to distressIns, they can't access its not static fields or methods, because it is null. So those two objects are unreachable and therefore egliable for garbage collection. You can't grab your hold on them again. – 3yakuya Sep 14 '14 at 18:54
  • Well, true. But the code does nothing at all with those two objects, so it's obviously just an example. In real code, there might be some way those objects are reachable, so I wanted to point that out. But still "become null" is the wrong term there. – markspace Sep 14 '14 at 18:56
  • Distress is a kind of singleton class in this example so if anyone is holding a ref then it would become null – Sunny Sep 14 '14 at 18:59
  • 2
    @Byakuya if someone is holding a reference to distressIns, then it can't be null, otherwise it wouldn't hold a reference to distressIns. Setting distressIns to null assigns null to the distressIns **variable**. The object still exists and can still be referenced by other variables. It will disappear from memory when it's not reachable anymore and the GC collects it. – JB Nizet Sep 14 '14 at 18:59
  • If any code EXTERNAL to the above code is holding a reference, making `distressIns` null does NOT cause the object to be garbage collected. That external code has to release its reference also. – markspace Sep 14 '14 at 19:00
  • 1
    @Sunny: no. The only thing that becomes null is the distressIns variable. Other variables that refer to the same object are unaffected. – JB Nizet Sep 14 '14 at 19:00
  • 1
    You can't null another object's reference to an object. Just because you null your local reference to an object, other references remain intact. So the `destroyIns` method will null this object's own reference, but it cannot null the reference held by any other object which called the `getIns` method of this object. – Bobulous Sep 14 '14 at 19:00
  • Guys please explain: how do you hold a reference to a private member of distressIns? – 3yakuya Sep 14 '14 at 19:04
  • `getIns()` is public, you just call it. – markspace Sep 14 '14 at 19:06
  • Because the `getIns` method returns the object reference. That reference points to the created object, so simply nulling the reference held by the original `Distress` object does not stop the other references from existing. – Bobulous Sep 14 '14 at 19:06
  • It returns the reference to a static member of the class, every object has a reference to the very same member of Distress. Am I wrong? Plus - there is still no way to access object's private members - you can't hold a direct reference to them. – 3yakuya Sep 14 '14 at 19:08
  • Weeell, the way he's creating `distressIns`, every call to `getIns()` will actually produce a different instance. So no most of them will have different references actually. But I realize it's just a quick example. – markspace Sep 14 '14 at 19:10
  • distressIns = new Distress(); return distressIns; You always reference the same field, it is the field that is changing its references. – 3yakuya Sep 14 '14 at 19:12
  • Um, but that field's value changes with each call, so each call will produce a new reference (and a new object). – markspace Sep 14 '14 at 19:13
  • I checked, you are right. Did not realize every reference will be different, although it was created by referencing the same field. Thank you. – 3yakuya Sep 14 '14 at 19:19
  • ok sorry bout that. since it is singleton there would be only one reference floating around everywhere. But still like it was mentioned, distressIns is made null, anyone else holding ref to this object would still remian intact right? – Sunny Sep 14 '14 at 19:20
  • 1
    @Byakuya so even if there is one instance floating around, if i make distressIns null, the others holding this single instance of Distress would still exist and hence it would not be eligible for GC. please help me understand if i am wrong here – Sunny Sep 14 '14 at 19:25
  • 1
    Yes, those other references are *copies* of the *value* of `distressIns`. Those copies don't go away because you set `distressIns` to null. – markspace Sep 14 '14 at 19:25
  • thanks. so is there any way to destroy the object which is being held by all the others via refernces? is that even possible? Or do the refernces have to be removed themselves – Sunny Sep 14 '14 at 19:27
  • Basically no, there's no way. If you need to solve some problem, please ask again, and show what that problem is. I think we've worn this question all out. – markspace Sep 14 '14 at 19:28