In Java, a constructor is a special method used to initialize objects. It is called when an instance of a class is created. Constructors have the same name as the class and do not have a return type, not even void. There are two types of constructors in the Java language. Default constructors and parameterized constructors. This article explains parameterized constructors.
What is Parameterized Constructor in Java?
A parameterized constructor is a constructor that takes one or more parameters to initialize an object with specific values when it is created. Using parameters, the developer can pass initial values to an object when it is instantiated. The parameterized constructors differ in terms of the parameters they hold. The compiler will not create a default constructor if the programmer creates their constructor.
Syntax of Parameterized Constructor
class ClassName {
// Parameterized constructor
ClassName(dataType parameter1, dataType parameter2) {
// Constructor body
}
}
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure
Why Are Parameterized Constructors Used?
The Java class creates a default constructor if the programmer hasn’t defined any type of constructor in the class. The parameterized constructor has to be defined explicitly by the programmer. The main aim of creating a parameterized constructor is to initialize the objects with programmer-defined states or values. Members can be assigned during the initialization process with the help of parameterized constructors.
Examples
One can have any number of parameterized constructors in a class.
Example of Parameterized Constructor
The following program demonstrates the parameterized constructor in Java. Three constructors are implemented: one is a default constructor, and the other two are parameterized constructors.
During an object’s initialization, which constructor should be invoked depends upon the parameters passed. For example, when we create an object like nameClass ob = new nameClass(“Harry”), the new keyword invokes parameterized constructors with a string parameter (nameClass(String)) after object initialization.
Program
class Employee{
private String name ;
private String age ;
Employee(){
System.out.println("Default Constructor");
}
Employee(String name ){
this.name = name ;
System.out.println("Parameterized Constructor with one String"+name);
}
Employee(String name, int age){
this.name = name ;
this.age = age ;
System.out.println("Parameterized Constructor with one string and number"+name+" "+age);
}
}
class Main{
public static void main(String args[]){
Employee ob = new Employee();
Employee ob2 = new Employee("Neeraj Kumar") ;
Employee ob3 = new Employee("Harry Potter",21) ;
}
}
Output
Default Constructor
Parameterized Constructor with one StringNeeraj Kumar
Parameterized Constructor with one string and numberHarry Potter 21
Example
Let’s look at some examples of parameterized constructors in Java.
Program
class Car {
String brand;
int year;
// Parameterized constructor
Car(String carBrand, int carYear) {
brand = carBrand;
year = carYear;
}
void displayInfo() {
System.out.println("Brand: " + brand + ", Year: " + year);
}
}
public class Main {
public static void main(String[] args) {
Car car1 = new Car("Toyota", 2020);
Car car2 = new Car("Honda", 2022);
car1.displayInfo();
car2.displayInfo();
}
}
If the programmer forgets to write a default constructor in the class, the compiler inserts the default constructor on its own. But if the programmer has written a constructor in the class, the compiler will not insert the default constructor in that class.
The following codes demonstrate the default constructor.
Program
class PrintingExample{
void printOnScreen(){
System.out.println("In this class, there is not constructor");
}
}
class Main{
public static void main(String args[]){
PrintingExample ob = new PrintingExample() ;
ob.printOnScreen();
}
}
Output
In this class, there is not constructor
Program 2
class Student{
Student(int a){
System.out.println("Parameterized Constructor with 1 integer argument") ;
}
}
class Main{
public static void main(String args[]){
Student ob = new Student(44) ;
}
}
Difference Between Default Constructor and Parameterized Constructor in Java
Default Constructor
Parameterized Constructor
This constructor does not support any parameter
This constructor takes one or more parameters
This initializes the object with default values
This constructor initializes the objects with custom values passed as arguments.
It is less flexible since all objects are initialized with the same default values.
The more flexible allows initializing objects with different values.
Person ob = new Person()
Person ob2= new Person(“Harry”, 30) ;
Conclusion
Parameterized constructors are very useful tools in Java for the creation of objects with definite information at the stage of their creation. It helps developers to influence the state of an object by setting a value for the field and making the source code more understandable and orderly. Constructor overload allows the development of more than one constructor because each one is characteristic of a particular occasion of initialisation. Its use makes the design of your classes more flexible, efficient and easier to work with, particularly where complex applications are inherent.
FAQs
What is a parameterized constructor in Java?
A parameterized constructor in Java accepts arguments (parameters) to initialize object attributes when the object is created.
Can a Java class have both default and parameterized constructors?
Yes, a Java class can have both default and parameterized constructors. This is called constructor overloading, where multiple constructors have different parameter lists.
Can a parameterized constructor be overloaded?
Yes, A parameterised constructor can be overloaded by defining multiple constructors with different parameter lists in the same class.
Can a parameterized constructor call another constructor in the same class?
Yes, It can be done using the this() keyword to call another constructor from within a constructor.
Can parameterized constructors have default values for parameters?
No, Java does not support default parameter values like some other languages. However, you can achieve similar functionality by overloading constructors with different parameter sets.
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.