visit [Link].
com for more study material
Objects dynamically allocated from a pool of free memory using new operator Possible for new to fail insufficient memory Key: Recovery of free memory from unused objects, making memory available for subsequent reallocation
visit [Link] for more study material
C++ : delete operator to free memory
Java: Garbage automatically collection techniquereclaims objects
When no reference to an object exists, that object is assumed to be no longer used and memory occupied by object is recycled
Occurs only sporadically during execution of a program It will not occur simply because one or more objects exist that are no longer used
visit [Link] for more study material
2 conditions are met
Objects to recycle need to recycle them Overhead : JVM does it only when necessary cannot know precisely collection will occur when garbage
visit [Link] for more study material
Before an object is removed by garbage collection, finalize() method is invoked to give it a last opportunity to clean up its act and free other kind of resources it may hold
e.g. closing files , terminating network connections , non window resources ensures that object terminates cleanly
visit [Link] for more study material
finalize() method is called once and only once before the object is garbage collected
No guarantee when this will happen may never run on a system that is not short of memory Finalization and Collection occur in two distinct phases of garbage collection process First items are finalized and then collected
visit [Link] for more study material
Syntax
protected void finalize() { //finalization code here }
protected: access specifier that prevents access to finalize() by code defined outside its class
visit [Link] for more study material
finalize() called before garbage collection
not called when object goes out of scope
visit [Link] for more study material
class FDemo { int x ; FDemo (int i ) { x=i; }
visit [Link] for more study material
protected void finalize() { [Link](Finalizing + x); }
void generator (int i) { FDemo o = new FDemo( i ); } }
visit [Link] for more study material
class Finalize { public static void main(String args[]) { int count; FDemo ob = new FDemo(o); for (count = 1 ; count < 100000; count++) [Link](count); } }
visit [Link] for more study material
Sometimes a method will need to refer to object that invoked it this keyword When a method is called it is automatically passed an implicit argument that is reference to invoking object. Reference is called this.
visit [Link] for more study material
class Point extends Object { public double x ; public double y; Point() { x = 0.0; y = 0.0; }
visit [Link] for more study material
Point (double x , double y) { this.x = x ; this.y = y; } }
visit [Link] for more study material