A default constructor is useful when an object can begin with the same basic state every time. But what if each object needs different data at the moment it is created? This is where a parameterized constructor becomes extremely useful.
A parameterized constructor allows you to pass values while creating an object. Instead of creating an empty object and assigning its fields afterward, you can create an object that is already initialized with the required information.
What Is a Parameterized Constructor?
A parameterized constructor is a constructor that accepts one or more parameters. These parameters are used to initialize the instance variables of the newly created object.
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
}
Here, Student(String name, int age) is a parameterized constructor because it accepts two values. The values supplied during object creation are used to initialize the object's state.
Why Do We Need Parameterized Constructors?
Suppose you are creating objects for 100 students. Giving every student the same default name and age would make little sense. You need a convenient way to provide different information for every object.
Student s1 = new Student("Rahul", 20);
Student s2 = new Student("Anita", 22);
Student s3 = new Student("Vikram", 21);
The same class can now create many objects, each with its own state. This is one of the biggest advantages of parameterized constructors: they make object creation flexible without requiring separate initialization statements.
Basic Syntax
class ClassName {
ClassName(dataType parameter1, dataType parameter2) {
// initialize instance variables
}
}
The constructor must still follow the normal constructor rules: its name must match the class name, and it must not have a return type.
Simple Example
Let's create a Student class that receives a student's name and age when the object is created.
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student("Rahul", 20);
student.display();
}
}
When new Student("Rahul", 20) executes, Java passes "Rahul" and 20 to the constructor. The constructor then stores those values in the newly created object's instance variables.
Name: Rahul Age: 20
How Parameter Passing Works
Consider this statement:
Student student = new Student("Rahul", 20);
The values supplied inside the parentheses become arguments. They are matched with the constructor parameters according to their position and compatible data types.
| Argument | Constructor Parameter | Value |
|---|---|---|
| "Rahul" | String name | Rahul |
| 20 | int age | 20 |
The parameter receives the argument value, and the constructor uses it to initialize the object.
Why Is this Used?
You may have noticed that the constructor parameters and instance variables often have the same names.
class Employee {
String name;
int salary;
Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
}
Here, name on the right side refers to the constructor parameter, while this.name refers to the current object's instance variable.
Important: The this keyword refers to the current object. It is especially useful when a constructor parameter has the same name as an instance variable.
Parameterized Constructor Without this
Using different parameter names can make the distinction obvious, although this is often preferred when the names naturally match.
class Employee {
String name;
int salary;
Employee(String employeeName, int employeeSalary) {
name = employeeName;
salary = employeeSalary;
}
}
This code works correctly because employeeName and employeeSalary clearly refer to the constructor parameters.
Creating Multiple Objects
One parameterized constructor can initialize many objects with different values.
class Product {
String name;
double price;
Product(String name, double price) {
this.name = name;
this.price = price;
}
}
public class Main {
public static void main(String[] args) {
Product p1 = new Product("Laptop", 65000);
Product p2 = new Product("Phone", 30000);
Product p3 = new Product("Tablet", 25000);
System.out.println(p1.name + " - " + p1.price);
System.out.println(p2.name + " - " + p2.price);
System.out.println(p3.name + " - " + p3.price);
}
}
Laptop - 65000.0 Phone - 30000.0 Tablet - 25000.0
The class remains the same, but each object contains different data. This is exactly the kind of flexibility constructors are designed to provide.
Constructor With Multiple Parameters
A parameterized constructor can accept as many parameters as the class design reasonably requires.
class Employee {
String name;
int age;
String department;
double salary;
Employee(String name, int age, String department, double salary) {
this.name = name;
this.age = age;
this.department = department;
this.salary = salary;
}
void display() {
System.out.println(name);
System.out.println(age);
System.out.println(department);
System.out.println(salary);
}
}
public class Main {
public static void main(String[] args) {
Employee employee = new Employee(
"Anita",
28,
"Development",
75000
);
employee.display();
}
}
This approach creates a fully initialized object in a single statement. In real applications, this can make object creation clearer and reduce the possibility of forgetting to initialize an important field.
Parameter Order Matters
Constructor arguments are matched according to their position. If you accidentally place values in the wrong order and their data types are compatible, Java may accept the code even though the resulting object contains incorrect information.
class Employee {
String name;
String department;
Employee(String name, String department) {
this.name = name;
this.department = department;
}
}
Employee employee = new Employee(
"Development",
"Anita"
);
The code may compile because both arguments are String values, but the object's data is reversed. This is a subtle bug that can be difficult to notice in larger applications.
Remember: Java checks argument compatibility, not whether your argument order makes business sense. When multiple parameters have the same data type, pay particular attention to their order.
What Happens If Arguments Are Missing?
If the constructor requires arguments, you must provide compatible arguments when creating the object.
class Car {
String model;
int year;
Car(String model, int year) {
this.model = model;
this.year = year;
}
}
Car car = new Car();
This code does not compile because the class has no no-argument constructor. It only has Car(String model, int year).
If your design needs both ways of creating an object, you must provide both constructors explicitly.
Combining No-Argument and Parameterized Constructors
class Car {
String model;
int year;
Car() {
model = "Unknown";
year = 0;
}
Car(String model, int year) {
this.model = model;
this.year = year;
}
}
public class Main {
public static void main(String[] args) {
Car car1 = new Car();
Car car2 = new Car("Toyota", 2026);
System.out.println(car1.model);
System.out.println(car2.model);
}
}
This is constructor overloading: the class provides multiple constructors with different parameter lists. The details of constructor overloading and chaining become especially useful as your classes become more sophisticated.
Common Beginner Mistakes
Adding a Return Type
class Student {
void Student(String name) {
this.name = name;
}
}
This is not a constructor because void makes it a method. A constructor never has a return type.
Forgetting this When Names Collide
class Student {
String name;
Student(String name) {
name = name;
}
}
The assignment above assigns the parameter to itself. The instance variable remains unchanged. The correct version is:
Student(String name) {
this.name = name;
}
Passing Incompatible Arguments
class Student {
Student(String name, int age) {
}
}
Student student = new Student(20, "Rahul");
The argument order does not match the constructor's parameter types, so Java reports a compilation error.
Best Practices
- Use parameterized constructors when an object requires data at creation time.
- Initialize essential fields so the object starts in a valid state.
- Use this when constructor parameters have the same names as instance variables.
- Avoid unnecessarily large constructors with too many parameters.
- Keep constructor logic focused on initialization rather than complex business operations.
- Choose parameter order carefully, especially when several parameters share the same data type.
Interview Insight
A common interview question is: Can a constructor have parameters? Absolutely. A constructor can accept parameters just like a method, but it does not have a return type. A constructor with one or more parameters is called a parameterized constructor.
Another important question is: Does Java automatically create a default constructor when a parameterized constructor is declared? No. Once you declare a constructor, Java does not automatically add a no-argument constructor. If both forms are required, you must declare both.
Quick Revision
| Concept | Key Point |
|---|---|
| Purpose | Initialize objects with supplied values |
| Parameters | One or more values can be accepted |
| Arguments | Passed during object creation |
| this | Refers to the current object |
| Return Type | Constructors have no return type |
| Flexibility | Different objects can start with different values |
Final Thoughts
Parameterized constructors turn object creation into a meaningful initialization step. Instead of creating an object first and filling it with data later, you can require the necessary information at the moment the object comes into existence. This makes your classes easier to use, reduces incomplete object states, and prepares you for important concepts such as constructor overloading, constructor chaining, and the this constructor call.
