Wednesday, February 1, 2012

Constructor and Destructor of Java

Intriduction of constructor.
Constructor and destructor are two important concepts of object oriented programming language. Constructor is nothing but a special type of member function that invokes automatically when the object is created of the particular class. In Java memory allocation is possible using new operator.
Example
        // instantiate an object of the class cat
        Home myhome  = new Home();

Here Home is class name and myhome is object name. In this particular statement, the new operator is used to allocate dynamic memory from the heap, and also as the constructor to construct the object in that memory space.The difference between class and structure is class member creates in heap memory and structure member is creates in stack memory. For that, class member access is more efficient than structure member access.  The address of the memory containing the object is returned and assigned to the reference variable named myhome If the memory cannot be allocated due to some reason, an exception will be thrown. In JAVA you do not define a constructor when you define a new class, a default constructor that takes no parameters is defined on your behalf.
You can also define your own constructor with no argument needed. Defining a constructor is similar to defining a method, but must have the same name as the class, do not have a return type and must not have return statements. There are three types of Constructor in JAVA

1)Default constructor
Constructor with no argument is called default constructor.
Example:
     Class hello()
{
   Hello()
{
 System.out.println(“I am hello constructor”);
}
}



2) Parameterized constructor
Constructor which takes parameter during the time of invocation is called parameterized constructor.
Example
Class unit
{
 Private int n;
 unit(int var)   //Parameterized constructor.
{
  n=var;
}
}
3)Copy constructor 
Takes object as a argument. It copies one object into another object.
Example:
Class unit
{
  Int n;
 unit(unit a)   //Copy constructor.
{
  this.n=this.a;
}
}

3)Copy constructor 
Takes object as a argument. It copies one object into another object.
Example:
Class unit
{
  Int n;
 unit(unit a)   //Copy constructor.
{
  this.n=this.a;
}
}


Features of constructor:-
1) Constructor need not call, it calls automatically when object creates.
2) Constructor name and class name must be same.
3) Generally constructor is declared as public mode.
4) Constructor is used to initialize object.
5) Constructor can be overloaded.
6) Constructor may be virtual.
7) constructor of one class can call constructor of other class using super keyword.
Destructor and finalize() method 
A destructor is a special method typically used to perform cleanup after an object is no longer needed by the program. C++ supports destructors, but JAVA does not support destructors.
JAVA supports another mechanism for returning memory to the operating system when it is no longer needed by an object.
Sometimes an object will need to perform some action when it is destroyed. For example,
if an object is holding some non-Java resource such as a file handle or window character
font, 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. By
using finalization, you can define specific actions that will occur when an object is just
about to be reclaimed by the garbage collector.

Example of  finalize() method.
public class Thing {

public static int number_of_things = 0;
public String what;

public Thing (String what) {
        this.what = what;
        number_of_things++;
        }

protected void finalize () {     //Destructor function
        number_of_things--;
        }

}


Garbage Collection
Since objects are dynamically allocated by using the new operator, you might be
wondering how such 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
deallocation for you automatically. The technique that accomplishes this is called garbage
collection. It works like this: when no references to an object exist, that object is assumed
to be no longer needed, and the memory occupied by the object can be reclaimed. There is
no explicit need to destroy objects as in C++. Garbage collection only occurs sporadically
(if at all) during the execution of your program. It will not occur simply because one or more
objects exist that are no longer used. Furthermore, different Java run-time implementations
will take varying approaches to garbage collection, but for the most part, you should not
have to think about it while writing your programs.

3 comments:

  1. constractor and distractor different now only clear to me and example also very useful .keep it up besanttechnologies reviews |
    besanttechnologies reviews | besanttechnologies reviews

    ReplyDelete


  2. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharng this information,this is useful to me...
    Android training in chennai
    Ios training in chennai

    ReplyDelete
  3. Thanks for posting useful information.You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...Really it was an awesome article...very interesting to read..please sharing like this information......
    Web Design Development Company
    Mobile App Development Company

    ReplyDelete