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.