Copy Constructors in Java


Implement a copy constructor to create a new object by copying another object's state

  • The Student class has two instance variables, name and age, to represent the name and age of a student.
  • It includes a constructor Student(String n, int a) that initializes the name and age of a Student object with the provided values.
  • There's another constructor Student(Student other) which is a copy constructor. It takes another Student object other as input and copies its name and age to the new Student object being created.
  • The method studentDetails() prints out the name and age of a Student object.
  • The main method is the entry point of the program. Inside main, it creates a Student object student1 with name "Tiya" and age 23 using the first constructor. Then it creates another Student object student2 using the copy constructor, passing student1 as an argument. It then calls studentDetails() on student2 to print out its details.

Source Code

class Student
{
	String name;
	int age;
 
	Student(String n, int a)
	{
		name = n;
		age = a;
	}
 
	Student(Student other)
	{
		name = other.name;
		age = other.age;
	}
 
	void studentDetails()
	{
		System.out.println("Name : " + name);
		System.out.println("Age : " + age);
	}
 
	public static void main(String[] args)
	{
		Student student1 = new Student("Tiya", 23);
		Student student2 = new Student(student1); // Copy constructor
		student2.studentDetails();
	}
}

Output

Name : Tiya
Age : 23