English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn about Java constructors with the help of examples, how to create and use them, and different types of constructors.
In Java, each class has its constructor, which is automatically called when an object of the class is created. The constructor is similar to a method, but it is not actually a method.
OneJava MethodsAnd Java constructors are distinguished by their name and return type. The constructor has the same name as the class and does not return any value. For example,
class Test { Test() { //Constructor body } }
Here, Test() is a constructor. It has the same name as the class and has no return type.
class Test { void Test() { // Method body } }
Here, Test() is the same as the name of the class. However, it has a return type of void. Therefore, it is a method, not a constructor.
class Main { private int x; // Constructor body private Main(){ System.out.println("The constructor is called"); x = 5; } public static void main(String[] args){ //The constructor is called when an object is created Main obj = new Main(); System.out.println("The value of x = ") + obj.x); } }
Output:
The constructor is called The value of x = 5
In the above example, we have a private constructor named Main(). In the main method, we are creating an object of the class named obj.
Main obj = new Main();
In this process, the constructor will be called. Therefore, execute the print statement and initialize the variable x.
In Java, constructors can be divided into3Types:
Parameterless Constructor
Default Constructor
Parameter constructor
Java constructors can have or not have any parameters. If a constructor does not accept any parameters, it is called a no-argument constructor. For example,
private Constructor() { // Constructor body }
class Main { int i; //Constructor without parameters private Main(){ i = 5; System.out.println("Object created and i = ") + i); } public static void main(String[] args) { //The constructor is called without any parameters Main obj = new Main(); } }
Output:
Object created and i = 5
Here, the Main() function does not accept any parameters.
Did you notice that the access modifier of the Main() constructor is private(private)?
This is because the object is instantiated from the same class. Therefore, it can access the constructor.
However, if an object is created outside the class, the constructor must be declared as public to access it. For example:
class Company { String domainName; // Public constructor public Company(){ domainName = "oldtoolbag.com"; } } public class Main { public static void main(String[] args) { // Create an object in another class Company companyObj = new Company(); System.out.println("Domain name = ",+ companyObj.domainName); } }
Output:
Domain name = oldtoolbag.com
Read more: Java Access Modifiers
If no constructor is created, the Java compiler will automatically create an unparameterized constructor at runtime. This constructor is called the default constructor. The default constructor initializes all 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 | null |
class DefaultConstructor { int a; boolean b; public static void main(String[] args) { //Call the default constructor 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 values of variables a and b. However, when we create an object of the class, we can see in the output that these values have been initialized with some values.
This is because the Java compiler automatically creates a default constructor. The constructor initializes the values of variables a and b with default values 0 and false.
The above program is equivalent to:
class DefaultConstructor { int a; boolean b; //Private constructor private DefaultConstructor() { a = 0; b = false; } public static void main(String[] args) { //Call the constructor DefaultConstructor obj = new DefaultConstructor(); System.out.println("a = ", + obj.a); System.out.println("b = ", + obj.b); } }
Output:
a = 0 b = false
Similar to methods, we can pass arguments to the constructor. This type of constructor is called a parameterized constructor. For example,
private Constructor(arg1, arg2{...}, argn) { // Constructor body }
class Vehicle { int wheels; //Constructor accepting a single value private Vehicle(int wheels){ this.wheels = wheels; System.out.println(wheels + " wheeler vehicle created."); } public static void main(String[] args) { //Call the constructor by passing a single value Vehicle v1 = new Vehicle(2); Vehicle v2 = new Vehicle(3); Vehicle v3 = new Vehicle(4); } }
Output:
2 wheeler vehicle created. 3 wheeler vehicle created. 4 wheeler vehicle created.
In the above example, we have a constructor named Vehicle(). The constructor accepts a parameter named wheels.
In this case, when creating an object, we pass parameters to the constructor. And, based on the parameters, it is generating the output.
Similar to method overloading, we can also overload constructors in Java. If you are not familiar with method overloading, please visitJava Method Overloading.
In constructor overloading, there are two or more constructors with different parameters. For example,
class Company { String domainName; //Constructor without parameters public Company(){ this.domainName = "default"; } //Constructor with a single parameter public Company(String domainName){ this.domainName = domainName; } public void getName(){ System.out.println(this.domainName); } public static void main(String[] args) { //Call the constructor without parameters Company defaultObj = new Company(); //Call the constructor with a single parameter Company w3codeboxObj = new Company("oldtoolbag.com"); defaultObj.getName(); w3codeboxObj.getName(); } }
Output:
default oldtoolbag.com
In the above example, we have two constructors: public Company() and public Company(String domainName).
Here, both constructors initialize the variable domainName with different values.Therefore, based on the values we need, we can call the constructor from the main() method.
Note that we use the this keyword to specify class variables. For more information about the this keyword, please visitJava This Keyword.
Constructors are implicitly called when instantiating objects.
The two rules for creating a constructor are:
The name of the constructor should be the same as the name of the class.
Java constructors must not have a return type.
If a class does not have a constructor, the Java compiler will automatically create one at runtimeDefault Constructor.The default constructor initializes instance variables with default values. For example, an int variable will be initialized to 0
Constructor Types:
Parameterless Constructor - Constructors that do not accept any parameters
Default Constructor - If no constructor is explicitly defined, the Java compiler will automatically create one.
Parameterized constructors - Constructors that accept parameters
Constructors cannot be abstract, static, or final.
Constructor overloading is possible but cannot be overridden.