0

OK, here is the thing. I have multiple pointers to an object and I want to remove both of them. I have simplified it, below, so don't bother asking why on earth I would try the thing below :)

    String first = "ASSIGNED";
    String second = test;
    second = null;
    System.out.println(first);

In the example above, the output still results in "ASSIGNED". However, I want first to be no longer assigned. In the example above, it is easy, by setting it to null, but in my real example, this is not possible (first being passed into a method, where I want to remove the assingment).

EDIT:

So, I guess the question is, if it is possible to remove all pointers to a given object.

Apparently it was a bit vague what I was asking, so let's try to give a better example:

String first = "Assigned";
doSomethingAndRemove(first);

public void Remove(String string) {
   //Do something 
    //...and remove
   string = null;
}

The thing is that first still is referencing to "Assigned"... But from what I read in the answers so far, there is no way around this?

MWB
  • 1,830
  • 1
  • 17
  • 38
  • 1
    I doubt what you're trying to do is possible – MadProgrammer Sep 10 '15 at 05:22
  • 1
    Why do you want to do this? – Tim Biegeleisen Sep 10 '15 at 05:23
  • what is second = test ? and obviously first will print "ASSIGNED" as first reference to value in string pool still exists – virendrao Sep 10 '15 at 05:23
  • When you say removing the assignment, programmatic way would be to nullify the assignment and running a GC on it. Not sure about writing such one. – Karthik R Sep 10 '15 at 05:23
  • Sorry, the second line should read: String second = first; – MWB Sep 10 '15 at 05:24
  • there is a doubt in your code . in line 2 you wrote second= test i assume it as second = first. even if you did so. you just derefrenced second only. First is still pointing to the same object. – Sachin Sep 10 '15 at 05:24
  • 1
    read about `string constant pool` – Rustam Sep 10 '15 at 05:25
  • in your case following steps are happening: first points to "ASSIGNED" in string pool second also points to ASSIGNED now. you remove reference of second now however reference of first still exists – virendrao Sep 10 '15 at 05:28
  • 1
    In your real example, set first=null after passing it to the method, then in the method, set the parameter you passed the value of first, to null. Bingo. – Amila Sep 10 '15 at 05:28

3 Answers3

1

C# can do it using ref, but Java cannot. The following is the last resort in Java if you must do it.

public class Nullify
{
    public static void main(String[] args) {
        String[] first = { "ASSIGNED" };
        System.out.println("first[0] = " + first[0]);

        nullify(first);
        System.out.println("first[0] = " + first[0]);
    }

    private static void nullify(String[] array) {
        array[0] = null;
    }
}
Takahiko Kawasaki
  • 18,118
  • 9
  • 62
  • 105
1

No, it is not possible to remove all "pointers" to a given object, for one because Java doesn't have pointers, they are called references.

Neither Java nor the JVM knows all references to a given object. This is why the garbage collector has to scan everything to find unused objects.

You have to clear references, and you cannot clear a reference variable that was used to pass a parameter from within the called method, because Java is pass-by-value, meaning the reference (not the object) is copied.

Example:

public static void main(String[] args) {
    String var1 = "Hello";
    doSomething(var1); // The value of var1 is copied to param1
}
private static void doSomething(String param1) {
    param1 = null; // Only clears the param1 reference
    var1 = null; // Cannot do this, because doSomething() does not have access to var1
}

Only code in method main can change the reference value of var1.

Update

If you want method doSomething to be able to update var1 to a different value, incl. null, then a common practice is to simply return the updated value.

public static void main(String[] args) {
    String var1 = "Hello";
    var1 = doSomething(var1);
}
private static void doSomething(String param1) {
    // do something with param1
    return "Goodbye";
}

This of course only works if you didn't already have a different kind of return value, and only works for a single value.

Output (and In/Out) parameters can be simulated in Java using the holder pattern, e.g. see Output Parameters in Java

Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • I think I have found a solution. I would like to share it, but I don't seem to be able to reply to my own question. Aslo, you cannot really get any code in the comments – MWB Sep 10 '15 at 16:38
0
String s=ASSIGNED;
String s2=s;
String s2=null; // means just remove the link s2 is pointing to
System.out.println(s); // print ASSIGNED

seen the picture.

enter image description here

if you want to remove you have to assign null to all individual objects.

Rustam
  • 6,485
  • 1
  • 25
  • 25