/* related posts with thumb nails */

finalize () method:

A constructor is used to initialize an object when it is declared. This is known as initialization. Similarly, Java supports a concept called finalization, which is just opposite to initialization. Java run-time is an automatic garbage collecting system. It automatically frees up the memory resources used by the objects. But objects may hold other non-object resources such as file descriptors or window system fonts. The garbage collector cannot free these resources in order to free these resources we must use a finalizer method. This is similar to destructors in C++, The finalizer method is simply finalize() and can be added to any class. Java calls that method whenever it is about to reclaim the space for that object. The finalize method should explicitly define the tasks to be performed.

Before an object is garbage collected, the runtime system calls its finalize() method. The intent is for finalize() to release system resources such as open files or open sockets before getting collected.

finalize() is best used for the following transactions.

· Memory deallocations

· To free non-object resources

· To close file streams

· To close database connections

· To close network sockets.

Related Topics:

0 comments:

Post a Comment