Write a Java program to Implement the parameterized constructor


This is an example of a parameterized constructor in Java. A constructor is a special method that is called when an object is created, and is used to initialize the object's fields. In this case, the Demo class has a constructor that takes two integer parameters (num1 and num2) and sets the values of the class's n1 and n2 fields to the values of those parameters, respectively.

In the main method, an instance of the Demo class is created using this constructor with the values 30 and 40. Then, the printValues method of this instance is called, which simply prints out the values of n1 and n2.

Source Code

class Parameterized_Constructor
{
	public static void main(String args[])
	{
		Demo d = new Demo(30, 40);
		d.printValues();
	}
}
class Demo
{
	int n1;
	int n2;
	Demo(int num1, int num2)
	{
		n1 = num1;
		n2 = num2;
	}
	void printValues()
	{
		System.out.println("Number 1 : " + n1);
		System.out.println("Number 2 : " + n2);
	}
}

Output

Number 1 : 30
Number 2 : 40