Parameterized Constructor in Java: Syntax, Examples

A parameterized constructor in Java is a constructor that accepts parameters to initialize instance variables with specific values when creating an object.

Unlike a default constructor in Java, which initializes objects with default values, a parameterized constructor allows you to pass values during object creation.

Syntax of Parameterized Constructor in Java

class ClassName {
// Declaration of instance variables.
datatype variable;
// Declaration of parameterized constructor with parameters.
ClassName(datatype parameter) {
variable = parameter;
}
}

In this example:

  • class is a keyword used to define a class in Java.
  • ClassName represents the name of the class.
  • ClassName(datatype parameter) defines the constructor with parameters.
  • The constructor must have the same name as the class.
  • Constructors do not have a return type (not even void).
  • Parameters allow passing values to initialize objects during object creation.

Single Parameterized Constructor in Java

When a constructor accepts only one parameter to initialize an instance variable, it is called a single-parameterized constructor in Java. Let us take a simple example program of single parameterized constructor in Java.

Example:

class Student {
int id;
// Declare single parameterized constructor with one parameter i of type int.
Student(int i) {
id = i;
}
// Declare an instance method.
void display() {
System.out.println("Student ID: " + id);
}
public static void main(String args[]) {
// Create an object of class Student and pass integer value to its single parameterized constructor.
Student s1 = new Student(101);
// Access the instance method.
s1.display();
}
}

Output:

Student ID: 101

Multiple Parameterized Constructor in Java

When a constructor accepts more than one parameter to initialize multiple variables, it is called multiple parameterized constructor in Java. Let’s take a simple example program based on multiple parameterized constructor.

Example: Two Parameterized Constructor

class Student {
int id;
String name;
// Declare two parameterized constructor with parameters i and n.
Student(int i, String n) {
id = i;
name = n;
}
void display() {
System.out.println(id + " " + name);
}
public static void main(String args[]) {
// Create an object of class and pass integer and string argument values to its constructor.
Student s = new Student(102, "Rahul");
// Accesss method to display message.
s.display();
}
}

Output:

102 Rahul

Example: Three Parameterized Constructor

class Employee {
// Declare instance variables.
int id;
String name;
double salary;
// Declare three parameterized constructor with parameters i, n, and s. 
Employee(int i, String n, double s) {
id = i;
name = n;
salary = s;
}
void display() {
System.out.println(id + " " + name + " " + salary);
}
public static void main(String args[]) {
// Creating multiple objects with passing different values.
Employee e1 = new Employee(1, "Amit", 50000);
Employee e2 = new Employee(2, "Neha", 60000);
// Accessing method using two different reference variables e1 and e2.
e1.display();
e2.display();
}
}

Output:

1 Amit 50000
2 Neha 60000

Using this Keyword with Parameterized Constructor

Sometimes parameter names are the same as instance variables. In this case, we use the this keyword.

Example:

class Car {
String model;
int price;
// Declare two parameterized constructor.
Car(String model, int price) {
this.model = model;
this.price = price;
}
void display() {
System.out.println(model + " " + price);
}
public static void main(String args[]) {
Car c1 = new Car("Toyota", 1500000);
c1.display();
}
}

Output:

Toyota 1500000

Advanced Example: Parameterized Constructor with Objects

class Rectangle {
int length;
int width;
Rectangle(int l, int w) {
length = l;
width = w;
}
int area() {
return length * width;
}
public static void main(String args[]) {
Rectangle r1 = new Rectangle(10, 5);
Rectangle r2 = new Rectangle(7, 3);
System.out.println("Area: " + r1.area());
System.out.println("Area: " + r2.area());
}
}

Output:

Area: 50
Area: 21

Real-World Examples of Parameterized Constructor

Let us take some real-world examples in which we will use parameterized constructor to initialize instance variables.

Example: Banking System

class BankAccount {
String accountHolder;
double balance;
BankAccount(String name, double amount) {
accountHolder = name;
balance = amount;
}
void display() {
System.out.println(accountHolder + " Balance: " + balance);
}
public static void main(String args[]) {
BankAccount acc1 = new BankAccount("Deepak", 10000);
acc1.display();
}
}

Output:

Deepak Balance 10000

Example: Online Shopping Product

class Product {
String productName;
int price;
Product(String name, int p) {
productName = name;
price = p;
}
void display() {
System.out.println(productName + " Price: " + price);
}
public static void main(String args[]) {
Product p1 = new Product("Laptop", 60000);
p1.display();
}
}

Output:

Laptop Price: 60000

Advantages of Parameterized Constructors in Java

There are the following advantages of parameterized constructors in Java. They are:

  • A parameterized constructor helps to initialize objects with custom values at the creation of object.
  • It improves code readability because initialization becomes clear and structured.
  • Parameterized constructor reduces the need for setter methods because we can assign values directly during object creation.
  • It allows you to support constructor overloading.

Default Constructor vs Parameterized Constructor

ParametersNo parametersAccepts parameters
InitializationDefault valuesCustom values
FlexibilityLess flexibleMore flexible
UsageBasic objectsReal-world objects

Frequently Asked Questions on Parameterized Constructor

1. What is a parameterized constructor in Java?

A parameterized constructor is a constructor that accepts one or more parameters to initialize object variables during object creation.

2. Can a class have multiple parameterized constructors?

Yes. Java supports constructor overloading that allows a class to have multiple constructors with different parameters.

3. Can we use this keyword in parameterized constructors?

Yes, we commonly use this keyword to distinguish instance variables from parameters. For example: this.name = name;

4. Can we call one constructor from another?

Yes. We can use this() to call another constructor inside the same class.

5. Can parameterized constructors be private?

Yes. Private constructors are used in the Singleton Design Pattern to restrict object creation.

Please Share Your Love.

Leave a Reply

Your email address will not be published. Required fields are marked *