File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Constructor and destructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor and destructor" Watch "Constructor and destructor" New topic
Author

Constructor and destructor

suganya raj
Greenhorn

Joined: Aug 08, 2007
Posts: 10
Hi,

What is difference between constructor and destructor?

Regards,
Suganya

EasyCalculation
ToFocus
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
In Java, there are no destructors. Maybe you are thinking of C++.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
A constructor allows you to define some intial conditions before your class is considered ready for use like, initializing variables, allocating some other required objects and assigning them to references, just anything that you need to do to make it ready for use.

A destructor is exactly the opposite... it gives the object a chance to grab any objects that need to be free'd prior to being freed itself. If care is not taken to "free" objects that have been used but are no longer required this is what is sometimes called a "memory leak". The memory allocated to that object is lost for use. It cannot be re-used for anything else because it is considered "still allocated" even though it is not in use. Generally, this type of program runs out of memory if it is something that runs for days without being restarted.


In Java a destructor is not considered required because the platform defines a garbage collector to recover "no-longer-used" memory. As soon as
an object becomes un-referenced (all references to it are nulled or destroyed themselves) then that object is elligible for garbage collection. In tight memory conditions the garbage collector can run, look for unreferenced objects and reclaim the memory for re-use. Code no longer needs to worry about free-ing unused objects.

Two things to think about...

1) if you declare static, or other long-lasting references to objects that you need for a while, and then forget about..... it is good practice to "null" the reference or those objects are held suspended in memory and cannot be reclaimed because the reference never goes away. One or two is one thing but, if you do a lot of it....with DIFFERENT reference variables ... then you are holding on to memory that could be released. In small, one-shot apps, it may not matter. But in heavy-use long running apps that make a practice of it...... it's can eventually hurt.

2) garbage collection demands it's price. It HAS to execute code to do it's job so, when it is time for it to run ( as in.... available heap space is getting low and you are needing more memory) it HAS to run and can impact the performance of your application.


------------------------
Bob
SCJP - 86% - June 11, 2009
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Bob Ruth:

2) garbage collection demands it's price. It HAS to execute code to do it's job so, when it is time for it to run ( as in.... available heap space is getting low and you are needing more memory) it HAS to run and can impact the performance of your application.


You make it sound as though this cost is peculiar to GC. But explicitly freeing objects with or without destructors, takes time too. It's just that with a GC, it possible that it will happen in big chunks at some inopportune time, while explicit destruction uniformly slows everything down.


[Jess in Action][AskingGoodQuestions]
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
That is true both schemes have to go through the overhead to release the memory. What is unique to GC is that it requires extra overhead to hunt down the objects elligible for garbage collection. In some manner it has to identify orphaned objects, round them all up, then release them. In non-GC language, when you are through with the object just delete it.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Bob Ruth:
... In non-GC language, when you are through with the object just delete it.

That seems simple enough. But failure to do this makes for a lot of bad software.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
Oh absolutely! I'm not debating that a bit. I've had to track down some memory leaks and there are lots of things that I'd rather do. I am not arguing against GC at ALL! Just noting that the "freedom" doesn't come free.
 
GeeCON Prague 2014
 
subject: Constructor and destructor