Join Our 4-Week Free Gen AI Course with select Programs.

Request a callback

or Chat with us on

Parameterized Constructor in Java: A Beginner’s Guide with Code Examples

Basics of Python
Basics of Python
icon
5 Hrs. duration
icon
9 Modules
icon
1800+ Learners
logo
Start Learning

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 } }

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(); } }

Output

Brand: Toyota, Year: 2020 Brand: Honda, Year: 2022
DevOps & Cloud Engineering
Internship Assurance
DevOps & Cloud Engineering

Error Thrown by Default Constructor

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) ; } }

Output

Parameterized Constructor with 1 integer argument

Also Read: Copy Constructor in Java

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
A parameterized constructor in Java accepts arguments (parameters) to initialize object attributes when the object is created.
Yes, a Java class can have both default and parameterized constructors. This is called constructor overloading, where multiple constructors have different parameter lists.
Yes, A parameterised constructor can be overloaded by defining multiple constructors with different parameter lists in the same class.
Yes, It can be done using the this() keyword to call another constructor from within a constructor.
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.

Deploying Applications Over the Cloud Using Jenkins

Prashant Kumar Dey

Prashant Kumar Dey

Associate Program Director - Hero Vired

Ex BMW | Google

19 October, 12:00 PM (IST)

Limited Seats Left

Book a Free Live Class

left dot patternright dot pattern

Programs tailored for your success

Popular

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
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.

Data Science

Accelerator Program in Business Analytics & Data Science

Integrated Program in Data Science, AI and ML

Accelerator Program in AI and Machine Learning

Advanced Certification Program in Data Science & Analytics

Technology

Certificate Program in Full Stack Development with Specialization for Web and Mobile

Certificate Program in DevOps and Cloud Engineering

Certificate Program in Application Development

Certificate Program in Cybersecurity Essentials & Risk Assessment

Finance

Integrated Program in Finance and Financial Technologies

Certificate Program in Financial Analysis, Valuation and Risk Management

Management

Certificate Program in Strategic Management and Business Essentials

Executive Program in Product Management

Certificate Program in Product Management

Certificate Program in Technology-enabled Sales

Future Tech

Certificate Program in Gaming & Esports

Certificate Program in Extended Reality (VR+AR)

Professional Diploma in UX Design

Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

© 2024 Hero Vired. All rights reserved