Skip to main content

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 
*************************