Constructor

Private data member of class can't be accessed outside the class. Every variable declared in java must be initialised before it's usage. Only way to manipulate class data member is method.


Two different type of constructor available in java

  1. Default Constructor
  2. Parameterised Constructor

Type 1 Default Constructor

Whenever a class not having any constructor then java provide a constructor without parameter to that class. Purpose of that constructor is to initialise all class's data member by default values. For every data type there are predefined default value.



Data type Default Value
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0
char \u0000
boolean false


Example:


 // file: default1.java 
// www.jgyan.com
class default1
{
byte b;
short s;
int i;
long l;
float f;
double d;
char c;
boolean b;
void show()
{
System.out.println("byte:"+b);
System.out.println("short:"+s);
System.out.println("int:"+i);
System.out.println("long:"+l);
System.out.println("float:"+f);
System.out.println("double:"+d);
System.out.println("char:"+c);
System.out.println("boolean:"+b);
}
}
class one
{
public static void main(String arg[])
{
defaultOne ob=new defaultOne();
ob.show();
}
}
/*output:
byte:0
short:0
int:0
long:0
float:0.0
double:0.0
char:
boolean:false
*/


Default constructor can be specified in class. A constructor without argument known as default constructor, once user will define constructor java stop providing constructor to that class.



Default constructor example:


 //file : complex.java 
// www.jgyan.com
class complex
{
private int real;
private int img;
// Default constructor: without parameter
complex()
{
real=1;
img=1;
}
public void show()
{
System.out.println("real:"+real);
System.out.println("img:"+img);
}
public void set(int r,int i)
{
real=r;
img=i;
}
}
class use
{
public static void main(String arg[])
{
complex comp1;
compl=new complex();
comp1.show();
}
}
/*output:
real:1
img:1
*/

Above example default constructor setting value of real and img variable to 1 and 1 respectively. Constructor is called automatically at the time of object creation. Every time a new object is created, object will be initialised by constructor.

Type 2 Parameterised Constructor

Default constructor set same value for all object. Some time we want to create object with different-2 value, to handle that situation we parameterized constructor come into role. Same like parameterized method, constructor can also have parameter. A class can have any number of constructor as long all those constructor are different in their parameter (type or number).



Example:


 // file: complex.java 
// www.jgyan.com
class complex
{
private int real;
private int img;
// Default constructor: without parameter
complex()
{
real=1;
img=1;
}
// Parameterized constructor: with one parameter
complex(int r)
{
real=r;
}
// Parameterized constructor: with two parameter
complex(int r,int i)
{
real=r;
img=i;
}
public void show()
{
System.out.println("real:"+real);
System.out.println("img:"+img);
}
public void set(int r,int i)
{
real=r;
img=i;
}
}
class use
{
public static void main(String arg[])
{
complex comp1;
compl=new complex(); // Default parameter will be called.
comp1.show();
comp1=new complex(19); // constructor with one parameter will be called
comp1.show();
comp1=new complex(2,3);// constructor with two parameter will be called
comp1.show();

}
}
/*output:
real:1
img:1
real:19
img:1
real:2
img:3
*/

In above example all constructor have same name but there behaviour are different. On the time of object creation parameters are used to differentiate constructor to be called.