In this tutorial, you'll learn about Java constructors, how to create and use them, and different types of constructors with the help of examples.
Constructors are used to initialize the object’s state. Like methods, a constructor also contains collection of statements(i.e. instructions) that are executed at time of Object creation. A Java method and Java constructor can be differentiated by its name and return type. A constructor has the same name as that of class and it does not return any value.
class CodeMistic
{
CodeMistic()
{
// constructor body
}
}
Here, CodeMistic() is a constructor. It has the same name as that of the class and doesn't have a return type.
class CodeMistic
{
private int x;
// constructor
private CodeMistic()
{
System.out.println("Constructor Called .");
x=5;
}
public static void main(String[] args)
{
// constructor is called while creating object
CodeMistic obj = new CodeMistic();
System.out.println("Value of x = " + obj.x);
}
}
Output :
Constructor Called .
Value of x = 5
There are three types of constructor in Java:
A Java constructor may or may not have any parameters (arguments). If a constructor does not accept any parameters, it is known as a no-argument constructor. For example,
class CodeMIstic
{
int i;
// constructor with no parameter
private CodeMIstic()
{
i = 5;
System.out.println("Object created and i = " + i);
}
public static void main(String[] args)
{
// calling the constructor without any parameter
CodeMIstic obj = new CodeMIstic();
}
}
Output :
Object created and i = 5
Here, the constructor CodeMistic() does not accept any parameters.
If you do not implement any constructor in your class, Java compiler inserts a default constructor into your code on your behalf. This constructor is known as default constructor. You would not find it in your source code(the java file) as it would be inserted into the code during compilation and exists in .class file.
The default constructor initializes any uninitialized instance variables with default values.
Type | Default Value |
---|---|
boolean | false |
byte | 0 |
short | 0 |
int | 0 |
long | 0L |
char | \u0000 |
float | 0.0f |
double | 0.0d |
object | Reference null |
For Example :
class DefaultConstructor
{
int a;
boolean b;
public static void main(String[] args)
{
// A default constructor is called
DefaultConstructor obj = new DefaultConstructor();
System.out.println("a = " + obj.a);
System.out.println("b = " + obj.b);
}
}
Output :
a = 0
b = false
In the above program, we have not initialized the value of both the variables, a and b. However, when we create an object of the class, we can see in the output that the values are initialized with some values. It is because the Java compiler has automatically created a default constructor. The constructor will initialize the value of variables a and b with default values 0 and false.
A constructor that has parameters is known as parameterized constructor. If we want to initialize fields of the class with your own values, then use a parameterized constructor.
For Example :
// Java Program to illustrate calling of
// parameterized constructor.
import java.io.*;
class CodeMistic
{
// data members of the class.
String name;
int id;
// constructor would initialize data members
// with the values of passed arguments while
// object of that class created.
CodeMistic(String name, int id)
{
this.name = name;
this.id = id;
}
}
class CM
{
public static void main (String[] args)
{
// this would invoke the parameterized constructor.
CodeMistic geek1 = CodeMistic("adam", 1);
System.out.println("Developer Name :" + geek1.name + " and Developer Id :" + geek1.id);
}
}
Output :
Developer Name :adam and Developer Id :1
Like methods, we can overload constructors for creating objects in different ways. Compiler differentiates constructors on the basis of numbers of parameters, types of the parameters and order of the parameters.
Example :
class Company
{
String domainName;
// constructor with no parameter
public Company()
{
this.domainName = "default";
}
// constructor with single parameter
public Company(String domainName)
{
this.domainName = domainName;
}
public void getName()
{
System.out.println(this.domainName);
}
public static void main(String[] args)
{
// calling the constructor with no parameter
Company defaultObj = new Company();
// calling the constructor with single parameter
Company CodeMisticObj = new Company("codemistic.in");
defaultObj.getName();
CodeMisticObj.getName();
}
}
Output :
default
codemistic.in