• 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

Constructor and destructor

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

What is difference between constructor and destructor?

Regards,
Suganya

EasyCalculation
ToFocus
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, there are no destructors. Maybe you are thinking of C++.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
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

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.
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic