2

For example, is this code valid?.

class abc{
    int x,y;
    abc(int x,int y){
        this.x=x;
        this.y=y;
        while(true)
            update();
    }

    public void update(){
        x--;
        y--;
        if(y==0)
            this=null;
    }
 }

If the above is not valid, then please explain why. I am in need of a class that after certain iterations ceases to exist. Please suggest alternatives to the above approach.

jigsawmnc
  • 444
  • 5
  • 15
  • A class' existence at runtime is because of it's object. What do you mean by ceases to exist? If it's object ceases to exist, that's it. And that's what gc does,doesn't it? Free references? – Kazekage Gaara May 16 '12 at 10:51
  • 5
    What are you expecting the `this=null` to do? – NPE May 16 '12 at 10:51
  • possible duplicate of [Can "this" ever be null in Java?](http://stackoverflow.com/questions/3789528/can-this-ever-be-null-in-java) – skaffman May 16 '12 at 10:54
  • The Garbage Collector takes care of collecting any unreferenced objects. I don't think you have to worry about that in Java. – adarshr May 16 '12 at 10:54
  • 1
    "Nullifying a class" does not mean anything. – Jesper May 16 '12 at 11:44

5 Answers5

7

No, this code is not valid.

Furthermore, I don't see what meaningful semantics it could have had if it were valid.

Please suggest alternatives to the above approach.

The object exists for as long as there are references to it. To make the object eligible for garbage collection you simply need to ensure that there are no references pointing to it (in your case, this should happen as soon as y reaches zero).

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • The loop will run forever. `update` needs to tell the constructor's while loop to terminate, perhaps by returning a boolean value: `return y != 0;` – David Harkness May 16 '12 at 11:06
5

No. The reason is that you do not make object null. When you say obj = null; You just put null to variable that previously hold reference to object. There are probably a lot of other references to the same object.

I think that what you want to do is to kind of invalidate object and make it garbage collected but take this decision inside the class. If this is the problem I'd recommend you to take a look on weak references.

Other possible solution is to implement kind of "smart reference" in java. You can create your class SmartReference that will hold the real reference to the object. The object should hold callback to this smart reference and call its method invalidate() that is something like your syntactically wrong expression this = null. You have to care not to refer to such objects directly but only via smart reference.

The only question is "why do you want to do this?". Really, this will cause the code to be more complicated and unstable. Imagine: the object decides to invalidate itself, so the reference that "smart reference" is holding becomes null. Now all holders of this smart reference will get NPE when trying to use the object! This is exactly the reason the such mechanism does not exist in java and that application programmer cannot mange the memory directly.

Bottom line: remove all object references and let GC to do its hard job. Trust it. It knows to clean the garbage.

AlexR
  • 114,158
  • 16
  • 130
  • 208
1

I think this is a good question. I've had loads of cases where I'd like Objects to validate themselves after/during construction and if it finds reason to, to just return an empty value or go back up the stack and skip over creating that object.

Mostly in the case of where you are creating a list of objects from a list of other values. If a value is garbage and you want your object to recognise this.

Rather then have to code a function outside the Class itself to validate the creation, it would be much neater to allow the object to do it.

It's a shame java doesn't allow for things like this on the assumption the programmer is probably going to mess it up. If you code well it would be a nice feature.

TheOnlyWAR10CK93
  • 113
  • 1
  • 10
0

I think you need to rethink why you want to do this, because what you're suggesting doesn't even exist as a concept in Java.

The this variable always refers to the object itself. You can't "nullify" an object, only a reference (since after all, what you're doing is assigning a reference to point to null instead of its previous object). It wouldn't make sense to do that with this, as it's always a pointer to the current object in scope.

Are you trying to force an object to be destroyed/garbage collected? If so, you can't do that while other parts of your code still have references to it (and if they don't have references, it will be garbage collected anyway).

What did you hope/think this would do, anyway?

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
0

your code must be get compile time error.. Coz..

The left-hand side of an assignment must be a variable

this is not a variable its a keyword..

this=null;
Sumit Singh
  • 15,743
  • 6
  • 59
  • 89