Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
280 views
in JAVA by (178k points)
edited by
What is a parameterized constructor in Java?

Please log in or register to answer this question.

2 Answers

0 votes
by (178k points)

A parameterized constructor is a type of constructor in Java that takes one or more parameters. The purpose of a parameterized constructor is to initialize the member variables of a class with the values passed to it during the creation of an object.

Here's an example of a parameterized constructor:

public class MyClass {
   int num;
   String str;

   public MyClass(int num, String str) {
      this.num = num;
      this.str = str;
   }
}
 

In the above example, the MyClass class has a parameterized constructor that takes two parameters, an integer num and a string str. The constructor initializes the member variables num and str with the values passed to it.

When an object of MyClass is created using this constructor, the values of num and str are set to the values passed to the constructor:

MyClass obj = new MyClass(10, "Hello");
 

In this example, the object obj is created using the parameterized constructor with the values 10 and "Hello". The member variable num is initialized to 10, and the member variable str is initialized to "Hello".

Parameterized constructors are useful when you want to initialize the member variables of a class with specific values during object creation.

0 votes
by (15 points)
edited by

A parameterized constructor in Java is a constructor that accepts parameters to initialize an object with specific values during its creation. Unlike a default constructor (which takes no arguments), a parameterized constructor allows you to pass data directly to the object at the time of instantiation.

Features of a Parameterized Constructor:

  1. It has the same name as the class.
  2. It includes parameters in its definition.
  3. It is used to initialize instance variables with values provided by the user.

Benefits of Parameterized Constructors:

  1. Flexibility: Allows you to initialize objects with specific values.
  2. Encapsulation: Hides the implementation details of object initialization.
  3. Code Reusability: Multiple objects can be created with different values using the same constructor.

Related questions

0 votes
1 answer
0 votes
1 answer

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...