Parameterized Constructors in Java


Implement a parameterized constructor to initialize an object

The Person class in Java represents a simple entity with a name attribute. It contains a constructor that initializes the name attribute with the provided value. In the main method, we demonstrate creating an instance of the Person class with the name "Sam Kumar" and then print the name of the person. This class provides a basic example of creating and using a class with a constructor in Java.

Source Code

class Person
{
	String name;
 
	Person(String n)
	{
		name = n;
	}
 
	public static void main(String[] args)
	{
		Person person = new Person("Sam Kumar");
		System.out.println("Name : " + person.name);
	}
}

Output

Name : Sam Kumar