Unlike Java methods, a constructor has the same name as that of the class and does not have any return type. For example,
class Test { Test[] { // constructor body } }
Here,
class Main {
private String name;
// constructor
Main[] {
System.out.println["Constructor Called:"];
name = "Programiz";
}
public static void main[String[] args] {
// constructor is invoked while
// creating an object of the Main class
Main obj = new Main[];
System.out.println["The name is " + obj.name];
}
}
7 is a constructor. It has the same name as that of the class and doesn't have a return type.Recommended Reading: Why do constructors not return values
Example 1: Java Constructor
class Main {
private String name;
// constructor
Main[] {
System.out.println["Constructor Called:"];
name = "Programiz";
}
public static void main[String[] args] {
// constructor is invoked while
// creating an object of the Main class
Main obj = new Main[];
System.out.println["The name is " + obj.name];
}
}
Output:
Constructor Called: The name is Programiz
In the above example, we have created a constructor named
class Main {
private String name;
// constructor
Main[] {
System.out.println["Constructor Called:"];
name = "Programiz";
}
public static void main[String[] args] {
// constructor is invoked while
// creating an object of the Main class
Main obj = new Main[];
System.out.println["The name is " + obj.name];
}
}
8. Inside the constructor, we are initializing the value of the name variable.Notice the statement of creating an object of the Main class.
Main obj = new Main[];
Here, when the object is created, the
class Main {
private String name;
// constructor
Main[] {
System.out.println["Constructor Called:"];
name = "Programiz";
}
public static void main[String[] args] {
// constructor is invoked while
// creating an object of the Main class
Main obj = new Main[];
System.out.println["The name is " + obj.name];
}
}
8 constructor is called. And, the value of the name variable is initialized.Hence, the program prints the value of the name variables as
Constructor Called: The name is Programiz0.
Types of Constructor
In Java, constructors can be divided into 3 types:
- No-Arg Constructor
- Parameterized Constructor
- Default Constructor
1. Java No-Arg Constructors
Similar to methods, 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,
private Constructor[] {
// body of the constructor
}
Example 2: Java private no-arg constructor
class Main {
int i;
// constructor with no parameter
private Main[] {
i = 5;
System.out.println["Constructor is called"];
}
public static void main[String[] args] {
// calling the constructor without any parameter
Main obj = new Main[];
System.out.println["Value of i: " + obj.i];
}
}
Output:
Constructor is called Value of i: 5
In the above example, we have created a constructor
class Main {
private String name;
// constructor
Main[] {
System.out.println["Constructor Called:"];
name = "Programiz";
}
public static void main[String[] args] {
// constructor is invoked while
// creating an object of the Main class
Main obj = new Main[];
System.out.println["The name is " + obj.name];
}
}
8. Here, the constructor does not accept any parameters. Hence, it is known as a no-arg constructor.Notice that we have declared the constructor as private.
Once a constructor is declared
Constructor Called: The name is Programiz2, it cannot be accessed from outside the class. So, creating objects from outside the class is prohibited using the private constructor.
Here, we are creating the object inside the same class. Hence, the program is able to access the constructor. To learn more, visit Java Implement Private Constructor.
However, if we want to create objects outside the class, then we need to declare the constructor as
Constructor Called: The name is Programiz3.
Example 3: Java public no-arg constructors
class Company {
String name;
// public constructor
public Company[] {
name = "Programiz";
}
}
class Main {
public static void main[String[] args] {
// object is created in another class
Company obj = new Company[];
System.out.println["Company name = " + obj.name];
}
}
Output:
Company name = Programiz
Recommended Reading: Java Access Modifier
2. Java Parameterized Constructor
A Java constructor can also accept one or more parameters. Such constructors are known as parameterized constructors [constructor with parameters].
Example 4: Parameterized constructor
class Main {
String languages;
// constructor accepting single value
Main[String lang] {
languages = lang;
System.out.println[languages + " Programming Language"];
}
public static void main[String[] args] {
// call constructor by passing a single value
Main obj1 = new Main["Java"];
Main obj2 = new Main["Python"];
Main obj3 = new Main["C"];
}
}
Output:
class Main {
private String name;
// constructor
Main[] {
System.out.println["Constructor Called:"];
name = "Programiz";
}
public static void main[String[] args] {
// constructor is invoked while
// creating an object of the Main class
Main obj = new Main[];
System.out.println["The name is " + obj.name];
}
}
0In the above example, we have created a constructor named
class Main {
private String name;
// constructor
Main[] {
System.out.println["Constructor Called:"];
name = "Programiz";
}
public static void main[String[] args] {
// constructor is invoked while
// creating an object of the Main class
Main obj = new Main[];
System.out.println["The name is " + obj.name];
}
}
8. Here, the constructor takes a single parameter. Notice the expression,class Main {
private String name;
// constructor
Main[] {
System.out.println["Constructor Called:"];
name = "Programiz";
}
public static void main[String[] args] {
// constructor is invoked while
// creating an object of the Main class
Main obj = new Main[];
System.out.println["The name is " + obj.name];
}
}
1Here, we are passing the single value to the constructor. Based on the argument passed, the language variable is initialized inside the constructor.
3. Java Default Constructor
If we do not create any constructor, the Java compiler automatically create a no-arg constructor during the execution of the program. This constructor is called default constructor.
Example 5: Default Constructor
class Main {
private String name;
// constructor
Main[] {
System.out.println["Constructor Called:"];
name = "Programiz";
}
public static void main[String[] args] {
// constructor is invoked while
// creating an object of the Main class
Main obj = new Main[];
System.out.println["The name is " + obj.name];
}
}
2Output:
class Main {
private String name;
// constructor
Main[] {
System.out.println["Constructor Called:"];
name = "Programiz";
}
public static void main[String[] args] {
// constructor is invoked while
// creating an object of the Main class
Main obj = new Main[];
System.out.println["The name is " + obj.name];
}
}
3Here, we haven't created any constructors. Hence, the Java compiler automatically creates the default constructor.
The default constructor initializes any uninitialized instance variables with default values.
TypeDefault Value
Constructor Called: The name is Programiz5false
Constructor Called: The name is Programiz60
Constructor Called: The name is Programiz70
Constructor Called: The name is Programiz80
Constructor Called: The name is Programiz90L
Main obj = new Main[];
0\u0000Main obj = new Main[];
10.0fMain obj = new Main[];
20.0dMain obj = new Main[];
3Reference nullIn the above program, the variables a and b are initialized with default value 0 and
Main obj = new Main[];
4 respectively.The above program is equivalent to:
class Main {
private String name;
// constructor
Main[] {
System.out.println["Constructor Called:"];
name = "Programiz";
}
public static void main[String[] args] {
// constructor is invoked while
// creating an object of the Main class
Main obj = new Main[];
System.out.println["The name is " + obj.name];
}
}
4The output of the program is the same as Example 5.
Important Notes on Java Constructors
- Constructors are invoked implicitly when you instantiate objects.
- The two rules for creating a constructor are:
The name of the constructor should be the same as the class.
A Java constructor must not have a return type. - If a class doesn't have a constructor, the Java compiler automatically creates a default constructor during run-time. The default constructor initializes instance variables with default values. For example, the
Constructor Called: The name is Programiz
8 variable will be initialized to
6Main obj = new Main[];
- Constructor types:
No-Arg Constructor - a constructor that does not accept any arguments
Parameterized constructor - a constructor that accepts arguments
Default Constructor - a constructor that is automatically created by the Java compiler if it is not explicitly defined. - A constructor cannot be
7 orMain obj = new Main[];
8 orMain obj = new Main[];
9.Main obj = new Main[];
- A constructor can be overloaded but can not be overridden.
Constructors Overloading in Java
Similar to Java method overloading, we can also create two or more constructors with different parameters. This is called constructors overloading.
Example 6: Java Constructor Overloading
class Main {
private String name;
// constructor
Main[] {
System.out.println["Constructor Called:"];
name = "Programiz";
}
public static void main[String[] args] {
// constructor is invoked while
// creating an object of the Main class
Main obj = new Main[];
System.out.println["The name is " + obj.name];
}
}
5Output:
class Main {
private String name;
// constructor
Main[] {
System.out.println["Constructor Called:"];
name = "Programiz";
}
public static void main[String[] args] {
// constructor is invoked while
// creating an object of the Main class
Main obj = new Main[];
System.out.println["The name is " + obj.name];
}
}
6In the above example, we have two constructors:
class Main {
private String name;
// constructor
Main[] {
System.out.println["Constructor Called:"];
name = "Programiz";
}
public static void main[String[] args] {
// constructor is invoked while
// creating an object of the Main class
Main obj = new Main[];
System.out.println["The name is " + obj.name];
}
}
8 and private Constructor[] {
// body of the constructor
}
1. Here, both the constructor initialize the value of the variable language with different values.Based on the parameter passed during object creation, different constructors are called and different values are assigned.
It is also possible to call one constructor from another constructor. To learn more, visit Java Call One Constructor from Another.
Note: We have used
private Constructor[] {
// body of the constructor
}
2 keyword to specify the variable of the class. To know more about private Constructor[] {
// body of the constructor
}
2 keyword, visit Java this keyword.