In this tutorial, we will learn about the constructor of a class, how to create them, what is the difference between the default constructor and the constructor we define and finally what is constructor overloading?
Definition of the constructor
In Java, the constructor is a special method, which is used to initialize and return the object of the class to which it is defined. The constructor will have the same name as the class it defines, and they do not return a value.
For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.huongdanjava; public class Student { private String name; private int age; public Student(String name) { this.name = name; } } |
In the example above, we have defined a constructor for the Student class, as you can see that it has no return type and that its name is the same as the class to which it is defined.
When an object is initialized by calling its constructor with the new operator, it invokes the superclass constructor and all instance variables are initialized with their default values. Back to the above example, when initializing the Student object by calling:
|
1 2 3 4 5 6 7 8 |
package com.huongdanjava; public class Example { public static void main(String[] args) { Student student = new Student("Khanh"); } } |
then the value of the age variable in the Student object will have a default value of type int to 0.
Default constructor
If in an object we do not define a constructor, by default, Java will add a default constructor to our object.
For example, I defined the Student class as follows:
|
1 2 3 4 5 6 7 |
package com.huongdanjava; public class Student { private String name; private int age; } |
At this point, I can initialize the Student object by:
|
1 2 3 4 5 6 7 8 |
package com.huongdanjava; public class Example { public static void main(String[] args) { Student student = new Student(); } } |
Obviously, although I do not define any constructors in the Student object, I can still initialize this object.
The default constructor does not contain any parameters, and when called to initialize an object, it also calls the constructor of the superclass and initializes the instance variables.
In the case, we have defined a constructor for the class:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.huongdanjava; public class Student { private String name; private int age; public Student(String name) { this.name = name; } } |
Java will not automatically add the default constructor, and then if we try to initialize the object using the default constructor, then a compile error will happen:

Defines a constructor
We can define a constructor for any class so that all properties of the object include methods, instance variables can be called according to our definition.
For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.huongdanjava; public class Student { private String name; private int age; public Student(String name) { this.name = name; age = 30; } } |
We can use four access modifiers to define a constructor, and thus we can limit the scope of classs access from other classes.
What happens if we define a constructor with a return value type? As such, Java treats it as a normal method rather than as a constructor.
For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.huongdanjava; public class Student { private String name; private int age; public void Student(String name) { this.name = name; age = 30; } } |
The Student class now has only a default constructor added by Java. If we initialize the Student object with a name parameter, we get an error.

Constructor overloading
What is constructor overloading? That is when we define multiple constructors for a class and each constructor will have different parameters in terms of both the parameter number and the data type of the parameter.
For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.huongdanjava; public class Student { private String name; private int age; public Student(int age) { this.age = age; } public Student(String name) { this.name = name; } public Student(String name, int age) { this.name = name; this.age = age; } } |
The rule to define a constructor overloading is:
- Constructors, as they say, must be defined using different parameters in terms of both the number of arguments and the type of the parameter.
- The constructors are not defined differently only in the access modifier.
In the case where the class has multiple constructors and we want to call this constructor from another constructor then we must use the keyword this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.huongdanjava; public class Student { private String name; private int age; public Student(int age) { this.age = age; } public Student(String name) { this.name = name; } public Student(String name, int age) { this(name); this.age = age; } } |
this statement to call other constructors must be in the first line of the constructor, if it is after any statement, the code will be compiled with an error:

You can watch the video here:
