• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Delete an object

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an object of class X.
X obj = new X();
What do I do if I want to reinitialize obj? That is, delete the existing object and create a new one which has a name obj.
public void checkInitialize()
{
X obj = new X();
}
Every time I invoke method checkInitialize(), I should reinitialize the object.
Thanks.
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do I do if I want to reinitialize obj? That is, delete the existing object and create a new one which has a name obj.
public void checkInitialize()
{
X obj = new X();
}
Every time I invoke method checkInitialize(), I should reinitialize the object.

The short answer is that you have answered your own question with your code, -- if you create a new instance of a class with X obj = new X();, then the object that was previously created and pointed to by reference obj is no longer reachable and the JVM will eventually reclaim the space, -- there is no need to explicitely delete it, as garbage collection is automatic in Java.
The long answer is that it's easy to introduce a memory leak in Java, despite of its automatic garbage collector. For example, suppose you created a new instance of X and added it to a list (or any other container). Or perhaps you passed it to some method, and some other object Y now holds a reference to X. Now, if you make a new instance of X and use the same reference obj, the old object will not be garbage collected, since it would still be reachable through the container or object Y (assuming that the life time of Y or the container is longer than that of obj.
Hope that wasn't too complicated, -- perhaps someone else can explain this important concept better.
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to clarify, you cannot actually delete an object, that is something that is done by the garbage collector (gc). The gc periodically checks if there are any objects that are no longer being referenced by any other object, and if found, will remove them from memory.
But I don't think you really care about "deleting" an object; I think you're more concerned with how to create a new instance of an object and assign it to a variable (member).
Take a look at this code:
 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. That was very helpful.
But what do I do if there is indeed some other objects that are using this object? So basically, my question comes down to this-
I want to re-initialize this object X. If some other methods are using X, then the too bad-- they will just loose all old values and will have to begin from the beginning.
What do I do to achieve this?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might consider giving class X a "reinitialize()" method.
 
reply
    Bookmark Topic Watch Topic
  • New Topic