473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 5274
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Timo J | last post by:
Hi - Im sitting and trying to understand this OOP - and need to create a class wich can do the following.. ShowBlocks() - Displays the data wich is inside it - If empty creates a form wich sends...
2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
4
by: Joe Schmoe | last post by:
All I want to to be able to take a two-column DataReader (One column with the Item ID number, the other with Item Description text) and load it into a Windows Forms ComboBox (Set to DropDownList...
32
by: paul | last post by:
HI! I keep on getting this error and I have tried different things but I am not sure how to send the expiring date. The error that I am getting in Firefox 1.5 is "Error: expires.toGMTString is...
3
by: Grant Schenck | last post by:
Hello, I have a Windows Service developed in C# .NET. I'm making it a remote server and I can, via an IPC Channel, expose methods and call them from a client. However, I now want my remoted...
2
by: ronchese | last post by:
I'm noticing this since I started developing a website project: my session variables are getting empty! I did a real easy test, and I checked my session variable is getting empty!! What is...
2
by: rocketfire97 | last post by:
I'm trying to call a COM object using C# but having no luck getting values back for passed in ref objects. I've tried the same call using VB.NET and can get data back. How would I implement the...
1
by: ced69 | last post by:
having trouble getting marquee to work get object required errors tring t <title>This Month at the Chamberlain Civic Center</title> <link href="styles.css" rel="stylesheet"...
2
by: sony.m.2007 | last post by:
Hi, When i try to set a value for a session variable I'm getting a object refence not set error I tried two methods as below HttpContext.Current.Session.Add("AppStartTime", DateTime.Now);...
0
by: =?Utf-8?B?RmFicml6aW8gQ2lwcmlhbmk=?= | last post by:
I need to access classic ASP intrinsic objects and their properties from a ..net assembly wrapped to COM. The COM .net assembly is then instanciated from a classic ASP page with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.