Sunteți pe pagina 1din 5

Constructor and Destructor of Java IntrOduction 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 () { number_of_things--; }

//Destructor function

S-ar putea să vă placă și