Constructor in java

Constructor
  • Constructor is special member of class it is used to initialize data
    member of class at time of object creation.
  • Every class must have constructor either user define or
    compiler define(default constructor)
  • Constructor define by programmer is known user define constructor where as constructor define by compiler is known as default constructor.
  • If user not define constructor then compiler automatically define
Two types of Constructor
  1. User define
  2. Compiler define
  • Constructor without any parameter is known as zero parameter or no argument constructor
  • Constructor define with parameter is known as parametarized constructor.
  • parametarized constructor used to pass value in constructor.
  • Constructor name same as class name
  • Constructor has no return type
constructor overloading
  •  In a class we can define any number of constructor is know as overloaded
    constructor.
  •  Whenever we create overloaded constructor argument should be differ in
    terms argument type or argument length.
  •  The overloaded constructor are used to create object with different
    initialization
  • Constructor returns the object address which is constructed by constructor
    of class.
Example of Overload Constructor and parametarized constructor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package constructor;
class Demo1
 {
	int a=10;
	int k;
	double d;
 
	//zero argument constructor
	Demo1()
	{  
		k=12;
		d=12.4;	
	}
	// u can overload the constructor
	// but argument should differ in terms of argument 
        //                                     type or length of argument
 
 
	Demo1(int a) //Parameterized constructor
	{
		this.a=a;
	}
 
 } 
 
public class Sample1
{
	public static void main(String[] args)
	{
		System.out.println("*************************");
 
		Demo1 obj1=new Demo1();
 
		System.out.println("k value : "+obj1.k);
 
		System.out.println("d value :"+obj1.d);
 
		Demo1 obj2=new Demo1(12);
 
        System.out.println("a value : "+obj2.a);
 
        System.out.println("*************************");
	}
 
}
 
output:
 
*************************
k value : 12
d value :12.4
a value : 12
*************************

This() method

What is the use of This() method ?

  • Constructor of class call the another constructor of same class by
    this() statement
this() Statement
  •  this() statement must be a first statement of constructor
    and only one this() statement is allowed
  •  Recursive constructor call is not allowed
  •  In constructor either this() allowed or either super() but not both
Example of this statement
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package constructor;
class A
{
 
	A()
	{
		this(20);
		System.out.println("running no arg constructor ");
	}
 
	A(int a)
	{
		this(10.5);
		System.out.println("running int arg constructor");
 
	}
 
	A(double arg)
	{
 
		System.out.println("running double arg constructor");
 
	}
 
}
 
 
public class ThisStatement
 {
 
public static void main(String[] args)
 {
	System.out.println("*************************");
	A a=new A();
	System.out.println("*************************");
   }	
}
 
output:
*************************
running double arg constructor
running int arg constructor
running no arg constructor 
*************************