Constructors in JavaLast Updated : 31 Dec 2025 In Java, a constructor is a special method used to initialize objects when a class is created. In this chapter, we will learn about the constructors and their types with the help of examples. What is a Constructor in Java?A Constructor in Java is a block of codes like the method. It is called when an instance of the class is created. At the time of calling constructor, memory for the object is allocated in the memory. It is a special type of method which is used to initialize the object. Every time when we create an object by using the new keyword, it calls a default constructor. If there is no constructor available in the class. In such case, Java compiler provides a default constructor by default. Note: It is called constructor because it constructs the values at the time of object creation. It is not necessary to write a constructor for a class. Because Java compiler creates a default constructor if your class does not have any. Rules for Creating Java ConstructorHere are the rules, you must follow to create constructors in Java: 1. Constructor name must be the same as its class nameA constructor must have the same name as the class. This helps the Java compiler identify the constructor and use it while creating an object. Syntax: It has the following syntax: Here, Student() is the constructor because its name matches the class name Student. 2. A constructor must have no explicit return typeA constructor does not return any value, not even void. If you specify a return type, it will be treated as a normal method, not a constructor. Incorrect Example: Correct Example: Here, in the correct example, we removed the return type "void" so it makes Test() a valid constructor. 3. A Java constructor cannot be abstract, static, final, or synchronizedConstructors are used to create and initialize objects, so Java does not allow these modifiers with constructors. Incorrect Example: Correct Example: Here, in the correct example, we removed "static" keyword that makes Demo() constructor valid. Note: We can use access modifiers while declaring a constructor. It controls the object creation. A constructor may be private, protected, public or default. Types of Java ConstructorsThere are two types of constructors in Java:
![]() Let's learn these types of the constructors in detail with the help of syntaxes and appropriate examples. 1. Java Default ConstructorWhen a constructor does not have any parameter, is known as default constructor. SyntaxIt has the following syntax: Example of Default ConstructorIn this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of object creation. Output: Bike is created Rule: If there is no constructor in a class, compiler automatically creates a default constructor. ![]() Purpose of a default constructorThe default constructor is used to provide the default values to the object like 0, null, etc., depending on the type. Another Example: Displaying the default valuesOutput: 0 null 0 null In the above class, we are not creating any constructor so compiler provides us a default constructor. Here, 0 and null values are provided by default constructor. 2. Java Parameterized ConstructorA constructor that has a specific number of parameters is called a parameterized constructor. SyntaxUse of Parameterized ConstructorThe parameterized constructor is used to provide different values to distinct objects. However, you can provide the same values also. Example of Parameterized ConstructorIn this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor. Output: 111 Joseph 222 Sonoo Java Copy ConstructorJava does not support the copy constructor. However, we can copy the values from one object to another, like a copy constructor in C++. There are the following three ways to copy the values of one object into another:
Java Copy Constructor ExampleIn this example, we are going to copy the values of one object into another using a Java constructor. Output: 111 Karan 111 Karan Copying Values Without using a ConstructorWe can copy the values of one object into another by assigning the object's values to another object. In this case, there is no need to create the constructor. ExampleLet us take an example to demonstrate how to copy values without using a constructor in Java. Output: 111 Karan 111 Karan Constructor Overloading in JavaIn Java, a constructor is just like a method but without return type. It can also be overloaded like Java methods. Constructor overloading in Java is a technique of having more than one constructor with different parameter lists. They are arranged in a way that each constructor performs a different task. They are differentiated by the compiler on the basis of the number of parameters in the list and their types. Example of Constructor OverloadingLet us take an example to demonstratethe working of constructor overloading in Java. Output: 111 Karan 0 222 Aryan 25 Constructor ChainingConstructor chaining in Java is a practice where one constructor calls another constructor of the same class or a superclass during object creation. It is typically done using either this() to call another constructor in the same class or super() to call a constructor in the superclass. By centralizing common construction logic, constructor chaining helps reduce code redundancy and increases the code's readability. To read more Constructor Chaining in Java If you are a beginner to Java, it is better to skip this part because we will learn about this and super keywords later. Difference Between Constructor and Method in JavaThere are many differences between constructors and methods. They are given below.
Important Points about ConstructorsThere are several important points about constructors in Java. Some of them are as follows:
Next Topicstatic keyword in Java |
We request you to subscribe our newsletter for upcoming updates.