keyword in java

what is a keyword in the Java language
  • this keyword is used to refer to a current object?
  • this is always a reference to the object on which method was invoked.
  • this can be used to invoke the current class constructor.
  • this can be passed as an argument to another method.

Example :
class Box
 {
  Double width, weight, dept; 
  Box (double w, double h, double d)
  {
   this.width = w;
   this.height = h;
   this.depth = d;
  }
}
Here the this is used to initialize a member of a current object.

The this is used to call an overloaded constructor in java

class Car
{
 private String name;
 public Car()
 {
  this("BMW");    //oveloaded constructor is called.
 }
 public Car(String n)
 {
  this.name=n;   //member is initialized using this.
 }
}

The this is also used to call the Method of that class.

public void getName()
{
 System.out.println("Studytonight");
}

public void display()
{
 this.getName();
 System.out.println();
}

this is used to return current Object

public Car getCar()
{
 return this;
}

What is Garbage Collection

In Java destruction of an object from memory is done automatically by the JVM. When there is no reference to an object, then that object is assumed to be no longer needed and the memory occupied by the object is released. This technique is called Garbage Collection. This is accomplished by the JVM.
Unlike C++ there is no explicit need to destroy the object.
What is Garbage Collection













Can the Garbage Collection be forced explicitly?

No, the Garbage Collection cannot be forced explicitly. We may request JVM for garbage collection by calling System.gc()method. But This does not guarantee that JVM will perform the garbage collection.

Advantages of Garbage Collection

1-The programmer doesn't need to worry about dereferencing an object.

2-It is done automatically by JVM.

3-Increases memory efficiency and decreases the chances for a memory leak.


finalize() method

Sometime an object will need to perform some specific task before it is destroyed such as closing an open connection or releasing any resources held. To handle such situation finalize() method is used. finalize() method is called by garbage collection thread before collecting object. Its the last chance for any object to perform cleanup utility.
Signature of finalize() method

protected void finalize()
{
 //finalize-code
}

Some Important Points to Remember method is defined in
  1. finalize() java.lang.Object class, therefore it is available to all the classes s.
  2. finalize() the method is declare as proctected inside Object class s.
  3. finalize() method gets called only once by GC threads.

gc() Method

gc() method is used to call garbage collector explicitly. However gc() method does not guarantee that JVM will perform the garbage collection. It only request the JVM for garbage collection. This method is present in the System and Runtime class.

Example for gc() method


public class Test
{
    
    public static void main(String[] args)
    {
        Test t = new Test();
        t=null;
        System.gc();
    }
    public void finalize()
    {
        System.out.println("Garbage Collected");
    }
}
Output :
Garbage Collected


































Comments