07 October 2010

0 What is constructor in Java

What is constructor in Java?

A constructor is a special method that is called to create a new object. It is not mandatory to write a constructor for the class. Constructor can be treated as a readily available, implicit method in every class.

If a user defined constructor is available, it is called just after the memory is allocated for the object.

If no user defined constructor is provided for a class, the implicit constructor initializes the member variables to its default values.

Numeric data types are set to 0,
Char data types are set to null character(‘\0’),
Boolean data types are set to false,
Reference variables are set to null

      The user defined constructor is usually used to initialize the data members of the objects to some specific values, other than the default values. A constructor method will have the same name as that of the class. A constructor method will not have any return type, not even void.

Just like other methods, constructors also can be overloaded.

The constructor without any parameter is called a default constructor.


0 comments:

Feeds Comments

Please give your valuable comments.