Java This Keyword
When a method will need to refer to the object that invoked it. To allow this , java defines the this keyword. this can be used inside any method to refer to the current object. That is, this is always a reference to the object on which the method was invoked. You can use this anywhere a reference to an object of the current class type is permitted.
For Exp
Rectangle(double l, double w){
this.length = l;
this.width = w;
}
This version of Rectangle() operated exactly like the normal. the use of this is redundant, but perfectly correct.
Java Garbage Collection
Objects
are dynamically allocated by using the new operator, you might be
thinking how objects are destroyed and their memory released for later
reallocation. In some languages, such as C++, dynamically allocated objects must be manually released by use of a delete operator. Java takes a different approach; it handles De-allocation for you automatically. The technique that accomplishes this is called garbage collection.
Finalize() Method
Sometimes an object will need to perform some action when it is destroyed. If an object is holding some non-Java resource such as a file handle or window character front, then you might want to make sure these resources are freed before an object is destroyed. TO handle such situations, Java provides a mechanism called finalization. You can define specific actions that will occur when an object is just about to reclaimed by the garbage collector by using finalization. To add a finalize to a class, you simply define the finalize() method. The java run time calls that method whenever it is about to recycle an object of that class. Inside the finalize() method you will specify those actions that must be performed before an object is destroyed.
The general form of finalize() method is as under:
protected void finalize()
{
sample code
}
keyword protected is used to prevent access to finalize() by code defined outside its class.
The general form of finalize() method is as under:
protected void finalize()
{
sample code
}
keyword protected is used to prevent access to finalize() by code defined outside its class.
This keyword in java
ReplyDeletereally great information thanks
Post a Comment