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.

7 comments:

  1. please provide full example

    ReplyDelete
  2. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    health and safrety courses in chennai

    ReplyDelete

  3. Are you looking for the best
    Oracle training in Chennai
    here is the best suggestion for you, Infycle Technologies the best Software training institute to study to also other technical courses like Cyber Security, Graphic Design and Animation, Block Security, Java, Cyber Security, Oracle, Python, Big data, Azure, Python, Manual and Automation Testing, DevOps, Medical Coding etc., with technical trainers with amazing training with a peaceful environment. And we also provide 100+ Live Practical Sessions and Real-Time scenarios which helps you to easily get through the interviews in top MNC’s. for more queries approach us on 7504633633, 7502633633.

    ReplyDelete