471,316 Members | 1,336 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,316 software developers and data experts.

Getting rid of an object

Hi,
I've started using Java recently so the question may seem trivial.
Is it possible to get rid of an object by simply creating a method (let's
call it "die") in which one simply includes the instruction: "this = null;"
? Would that work if the object has been previously placed in a list of
similar objects? Do I need to go and delete it from the list or simply that
list would now reference a "null" object? If so what would the size() of
that list return in such case? Probably the same size I would guess. I would
also guess things would work as long as one can live with "null" objects in
the list.
Thx,
Alex
Jul 17 '05 #1
7 5094
Joe
In article
<4D*********************@news01.bloor.is.net.cable .rogers.com>,
al*************@hotmail.com says...
Hi,
I've started using Java recently so the question may seem trivial.
Is it possible to get rid of an object by simply creating a method (let's
call it "die") in which one simply includes the instruction: "this = null;"

To me, it sounds like a very efficient way to add bugs to your code at an
amazing rate.
You can do all this stuff, but it's exactly what the developers of the
language had hoped to relieve developers from the burden of handling. If
you really need to make an object inaccessible, I think it would be
preferrable to have a boolean instance variable called, for instance,
'active' which must have a value of true before any methods are allowed
to execute. Let the JVM worry about garbage-collection.


--
"It is impossible to be unjust or unfair to the rich and powerful"
-- Harry Britt
Jul 17 '05 #2

"Joe" <sf***@spamcop.net> wrote in message
news:MP************************@sfo.news.speakeasy .net...
In article
<4D*********************@news01.bloor.is.net.cable .rogers.com>,
al*************@hotmail.com says...
Hi,
I've started using Java recently so the question may seem trivial.
Is it possible to get rid of an object by simply creating a method (let's call it "die") in which one simply includes the instruction: "this =
null;"

To me, it sounds like a very efficient way to add bugs to your code at an
amazing rate.
You can do all this stuff, but it's exactly what the developers of the
language had hoped to relieve developers from the burden of handling. If
you really need to make an object inaccessible, I think it would be
preferrable to have a boolean instance variable called, for instance,
'active' which must have a value of true before any methods are allowed
to execute. Let the JVM worry about garbage-collection.

But that's exactly want I want to do. How can the garbage-collection worry
about it if there are still references to the object. GC would remove it if
there are no more references and this can be achieved by setting the pointer
to the object to NULL...
The flag solution is not very good in my case because I'm creating and
removing thousands (maybe hundred of thousands) of these things.

Alex
Jul 17 '05 #3
"Alexander Tulai" <al*************@hotmail.com> wrote in message news:<4D*********************@news01.bloor.is.net. cable.rogers.com>...
Hi,
I've started using Java recently so the question may seem trivial.
Is it possible to get rid of an object by simply creating a method (let's
call it "die") in which one simply includes the instruction: "this = null;"
? Would that work if the object has been previously placed in a list of
similar objects? Do I need to go and delete it from the list or simply that
list would now reference a "null" object? If so what would the size() of
that list return in such case? Probably the same size I would guess. I would
also guess things would work as long as one can live with "null" objects in
the list.
Thx,
Alex

No. You can't do that. "this" is a final variable, and you cannot
programmatically change it in an object.

Regards.
Jul 17 '05 #4
da*****@terracox.syzygy-tech.com (Dai Ichi) wrote in
news:e5**************************@posting.google.c om:

No. You can't do that. "this" is a final variable, and you cannot
programmatically change it in an object.


In addition to that, even it this were non-final, it still is just another
reference to your object, so by setting this to null, you would break just
that one reference (and possibly invalidate most of the code of your object
in the mean time, including code required to clean it up), but your queue
would still contain a reference to the object.
Jul 17 '05 #5
Joe
In article
<Tj*********************@news01.bloor.is.net.cable .rogers.com>,
al*************@hotmail.com says...
But that's exactly want I want to do. How can the garbage-collection worry
about it if there are still references to the object. GC would remove it if
there are no more references and this can be achieved by setting the pointer
to the object to NULL...
Then, what you want to do is to be able to programmatically remove all
the references to your objects?

The flag solution is not very good in my case because I'm creating and
removing thousands (maybe hundred of thousands) of these things.

Alex

So? The number of objects is irrelevant.
Jul 17 '05 #6
You are correct.
But wouldn't that (the idea , not necessarily the means proposed) be a
great idea of getting rid of objects without having to go around and clean
any structures referencing it?
Object Ox --->Ptr to Object Ox --> Ptr to Ptr. to Objexct Ox
The reference structure should use Ptr to Ptr. to Object Ox
If one wants to kill the object just makes Ptr to Object Ox = null.
Now, because there is no more references to Object Ox , the Garbage
Collector will take care of it.
Meantime, the rest of the structures using Ptr to Ptr. to Objexct Ox should
be smart enough to detect when Ptr to Object Ox == null and clean themself
up ....!
Thanks all for your feedback.
Alex

"Dai Ichi" <da*****@terracox.syzygy-tech.com> wrote in message
news:e5**************************@posting.google.c om...
"Alexander Tulai" <al*************@hotmail.com> wrote in message

news:<4D*********************@news01.bloor.is.net. cable.rogers.com>...
Hi,
I've started using Java recently so the question may seem trivial.
Is it possible to get rid of an object by simply creating a method (let's call it "die") in which one simply includes the instruction: "this = null;" ? Would that work if the object has been previously placed in a list of
similar objects? Do I need to go and delete it from the list or simply that list would now reference a "null" object? If so what would the size() of
that list return in such case? Probably the same size I would guess. I would also guess things would work as long as one can live with "null" objects in the list.
Thx,
Alex

No. You can't do that. "this" is a final variable, and you cannot
programmatically change it in an object.

Regards.

Jul 17 '05 #7
Hi,
Dai Ichi wrote:
...

No. You can't do that. "this" is a final variable, and you cannot
programmatically change it in an object.


The "this" variable is not a field, it is a local variable implicitly
defined in all non-static methods and, of course, always refers to
the instance upon which the method was invoked. Even if it could be
zeroed out, it would not affect garbage collection. It would merely
sever the connection between the method invocation and the instance on
which it was operating.

Randall Schulz

Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Timo J | last post: by
3 posts views Thread by Grant Schenck | last post: by
2 posts views Thread by ronchese | last post: by
2 posts views Thread by sony.m.2007 | last post: by
reply views Thread by =?Utf-8?B?RmFicml6aW8gQ2lwcmlhbmk=?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.