Parameterized Constructor in Java
A parameterized constructor is a constructor that accepts arguments, allowing you to initialize an object's fields with specific values at the time of its creation.
Syntax
class ClassName {
ClassName(parameterType parameterName) {
// initialization code
}
}
Example
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
}
Student s1 = new Student("Aman", 22);
Student s2 = new Student("Priya", 24);
Key Points
- Parameterized constructors let each object be initialized with different values right at creation
- Once a class defines a parameterized constructor, Java no longer provides a default no-arg constructor automatically
- A class can have multiple parameterized constructors through constructor overloading
Why Use Parameterized Constructors?
- Avoids the need to set every field manually with setters after creating the object
- Makes sure objects are created in a valid, fully-initialized state
- Improves readability, since the values passed during creation are visible at a glance
new ClassName() with no arguments, the code will not compile unless you also explicitly add a no-arg constructor.Master Java with Uncodemy
Hands-on training, live projects, and placement support in our Java Programming Course.
What Happens When new Student("Aman", 22) Runs
Knowing the order of events helps you debug fields that end up null or zero.
- The JVM allocates memory for a new Student object on the heap.
- Every field gets its default value:
nullfor references,0for numbers,falsefor booleans. - The parent class constructor runs first. If you do not write
super(...), Java inserts a call to the parent's no-argument constructor. - Field initializers and instance blocks run in the order they appear in the class.
- Finally the body of your parameterized constructor runs and copies the arguments into the fields.
- The reference to the finished object is returned and stored in
s1.
Validation, Chaining and final Fields
A constructor is the best place to reject bad data, because an object that never gets built cannot spread invalid state through your program. Combine that with this(...) to avoid repeating the same assignments in every overloaded constructor.
public class BankAccount {
private final String accountNumber;
private final String holder;
private double balance;
public BankAccount(String accountNumber, String holder, double openingBalance) {
if (accountNumber == null || accountNumber.isBlank()) {
throw new IllegalArgumentException("Account number is required");
}
if (openingBalance < 0) {
throw new IllegalArgumentException("Opening balance cannot be negative");
}
this.accountNumber = accountNumber;
this.holder = holder;
this.balance = openingBalance;
}
// Delegates to the main constructor with a zero balance
public BankAccount(String accountNumber, String holder) {
this(accountNumber, holder, 0.0);
}
}
Marking fields final forces them to be set exactly once, and the compiler checks that every constructor path does so. Note that this(...) must be the first statement in the constructor.
Parameterized constructors and inheritance
If a parent class only has a parameterized constructor, every child must call it explicitly, otherwise compilation fails because the implicit super() has nothing to call.
class SavingsAccount extends BankAccount {
private final double interestRate;
SavingsAccount(String number, String holder, double rate) {
super(number, holder); // required
this.interestRate = rate;
}
}
The Shadowing Bug and Other Mistakes
The most common beginner error is leaving out this when the parameter has the same name as the field:
Student(String name, int age) {
name = name; // assigns the parameter to itself
age = age; // fields stay null and 0
}
The code compiles without complaint, which makes the bug hard to spot. Other mistakes to avoid:
- Writing a return type such as
void Student(...). That turns it into an ordinary method, and the constructor is silently gone. - Calling overridable methods from a constructor. A subclass override may run before the subclass fields are initialized.
- Accepting a long list of parameters of the same type. When a constructor needs many arguments, consider a builder so callers cannot mix up the order.
Quick Checklist
- Use
this.field = parameterfor every assignment. - Validate arguments and throw early.
- Chain overloaded constructors with
this(...). - Add a no-argument constructor explicitly if a framework needs one.
- For simple data holders, a Java
recordgenerates the parameterized constructor for you.