<p>In this article, you&#8217;ll learn about Java constructors, how to create and use constructors with the help of examples.</p>
<h3><span style="color: #000080;"><strong>Constructor</strong></span></h3>
<ul>
<li>A constructor is a member function of a class with the same name as its class and has no return type.</li>
<li>A constructor for a class is a special method that is used to initialize a newly created object of that class type with a legal initial value.</li>
<li>It is called implicitly, just after the memory is allocated for the object.</li>
<li>It is not mandatory for the programmer to write a constructor for a class.</li>
<li>When there is no constructor defined in the class by the programmer, compiler implicitly provides a default constructor for the class.</li>
</ul>
<h3></h3>
<h3><span style="color: #000080;"><strong>Defining Constructor</strong></span></h3>
<p>There are two ways of defining a constructor:</p>
<ul>
<li>Without Arguments</li>
<li>With Arguments</li>
</ul>
<h4></h4>
<h4><strong><span style="color: #000080;">Constructor Without Arguments</span></strong></h4>
<p>A constructor that accepts no parameter is called <strong><span style="color: #008000;">default</span></strong> constructor.</p>
<p>If we do not define a constructor in a class, then the compiler will automatically create a default constructor (with no arguments) during runtime for the class, with data members which have values like zero, null, etc.</p>
<p>But, if we define a constructor in a class with arguments or no arguments, then the compiler does not create a <strong><span style="color: #008000;">default</span></strong> constructor.</p>
<p><span style="color: #0000ff;"><strong>Syntax:</strong></span></p>
<p>The syntax of no-argument constructor is:</p>
<pre>accessModifier ClassName() {
 <strong><span style="color: #008000;">// constructor body</span></strong>
}</pre>
<p> ;</p>
<p><span style="color: #0000ff;"><strong>Example: Defining no-argument Constructor in a class</strong></span></p>
<pre>class Student {
 int rollNo;
 String name;

 <span style="color: #000000;"> <strong>public Student() {
 rollNo = 5;
 name = "Arun Verma";
 }</strong></span>
}
 class Person {
 public static void main(String[] args) {
 Student student = new Student();
 System.out.println("Roll no :" + student.rollNo + "\n" + "Name :" + student.name);
 }
}
</pre>
<p><span style="color: #0000ff;"><strong>Output:</strong></span></p>
<pre>Roll no :5
Name :Arun Verma</pre>
<p><span style="color: #0000ff;"><strong>Note:</strong></span> To initialize the state of an object with some value we have to write the initialization statement inside the constructor otherwise the data members will initialize with the default datatype values like zero, null, etc.</p>
<p> ;</p>
<p><span style="color: #0000ff;"><strong>Example: Not defining any Constructor in a class</strong></span></p>
<pre>class Student {
 int rollNo;
 String name;
}

class Person {
 public static void main(String[] args) {
 Student student = new Student();
 System.out.println("Roll no :" + student.rollNo + "\n" + "Name :" + student.name);
 }
}</pre>
<p><span style="color: #000000;">The above program is equivalent to:</span></p>
<pre>class Student {
 int rollNo;
 String name;
<strong><span style="color: #008000;"> 
 //default constructor created by the compiler</span></strong>
 <span style="color: #000000;"><strong> public Student() {
 }</strong></span>
}

class Person {
 public static void main(String[] args) {
 Student student = new Student();
 System.out.println("Roll no :" + student.rollNo + "\n" + "Name :" + student.name);
 }
}</pre>
<p><span style="color: #0000ff;"><strong>Output:</strong></span></p>
<pre>Roll no :0
Name :null</pre>
<h4></h4>
<h4><strong><span style="color: #000080;">Constructor With Arguments</span></strong></h4>
<p>A constructor which consist of arguments is called a parameterized constructor.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<p>It is used to assign values to distinct objects of that class type.</p>
<p><span style="color: #0000ff;"><strong>Syntax:</strong></span></p>
<p>The syntax of parameterized constructor is:</p>
<pre>accessModifier ClassName(arg1,arg2,....,argn) {
 <strong><span style="color: #008000;"> // constructor body</span></strong>
}</pre>
<p> ;</p>
<p><span style="color: #0000ff;"><strong>Example: Constructor with arguments</strong></span></p>
<pre>class Student {
 int rollNo;
 String name;

 <strong>public Student(int roll_no, String student_name) {
 rollNo = roll_no;
 name = student_name;
 }</strong>

 void show() {
 System.out.println(rollNo + " : " + name);
 }

}
class Person {

 public static void main(String[] args) {
 Student s1 = new Student(101, "Arun Verma");
 Student s2 = new Student(102, "Rohit Sharma");
 Student s3 = new Student(103, "Pooja Singh");
 
 s1.show();
 s2.show(); 
 s3.show(); 
 }
}</pre>
<p><span style="color: #0000ff;"><strong>Output:</strong></span></p>
<pre>101 : Arun Verma
102 : Rohit Sharma
103 : Pooja Singh</pre>
<h3></h3>
<h3><span style="color: #000080;"><strong>Constructor Overloading</strong></span></h3>
<p>Just like method overloading, constructors can be overloaded to create objects in different ways.</p>
<p>The compiler differentiates constructors based on no. of arguments present in the constructor and other parameters like the order in which the arguments are passed.</p>
<p><span style="color: #0000ff;"><strong>Example: Constructor Overloading</strong></span></p>
<pre>class Student {
 int rollNo;
 String name;
 String subject;
 
<strong><span style="color: #008000;"> //no-argument constructor</span></strong>
 <strong> public Student() {
 rollNo = 101;
 name = "Arun Verma";
 subject = "Chemistry";
 }</strong>
<strong><span style="color: #008000;"> 
 //constructor with two parameters</span></strong>
 <strong> public Student(int roll_no, String student_name) {
 rollNo = roll_no;
 name = student_name;
 subject = "Physics";
 }</strong>
<strong><span style="color: #008000;"> //constructor with three parameters</span></strong>
 <strong>public Student(int roll_no, String student_name, String subject_name) {
 rollNo = roll_no;
 name = student_name;
 subject = subject_name;
 }</strong>

 void show() {
 System.out.println(rollNo + " : " + name + " : " + subject);
 }

}

class Person {
 public static void main(String[] args) {
 <span style="color: #008000;"><strong>//no-argument constructor called</strong></span>
 Student s1 = new Student();
<strong><span style="color: #008000;"> //constructor(with two parameters) called</span></strong>
 Student s2 = new Student(102, "Rohit Sharma");
 <strong><span style="color: #008000;">//constructor(with three parameters) called</span></strong>
 Student s3 = new Student(103, "Sandhya Singh", "Biology");

 s1.show();
 s2.show();
 s3.show();
 }
}</pre>
<p><span style="color: #0000ff;"><strong>Output:</strong></span></p>
<pre>101 : Arun Verma : Chemistry
102 : Rohit Sharma : Physics
103 : Sandhya Singh : Biology</pre>
<h3><span style="color: #000080;"><strong>How constructor different from methods</strong></span></h3>
<ul>
<li>A Constructor is used to initialize the state of an object whereas a method is used to expose the behavior (functionality) of an object.</li>
<li>Constructor must have the same name as the class within which it defined while it is not mandatory for the method in java.</li>
<li>Constructor has no return-type while methods have return-type or <span style="color: #008000;"><strong>void</strong></span> if it does not return any value.</li>
<li>Constructor is called only once at the time of Object creation while method can be called any numbers of time.</li>
</ul>


