Copy Constructor in Java

Constructors are special methods named after the class and without a return type, and are used to construct objects. Constructors, like methods, can take input parameters. Constructors are used to initialize objects. A copy constructor is a constructor that creates a new object using an existing object of the same class and initializes each instance variable of newly created object with corresponding instance variables of the existing object passed as argument.

Syntax :
   public class_Name(const className old_object)

Example :
   Public student(student o)

Source Code

import java.util.Scanner;
class CopyConstructor
	int age;
	String name;
	CopyConstructor(int a,String n)
	CopyConstructor(CopyConstructor cc)
	void display()
		System.out.println("Your name is : "+name + "\nAge is : "+age);
	public static void main(String[] arg)
		System.out.print("Enter your name and age :");
		Scanner scan  = new Scanner(;
		String name =scan.nextLine();
		int age =scan.nextInt();
		CopyConstructor cc = new CopyConstructor(age,name);
		CopyConstructor c2=new CopyConstructor(cc);


Enter your name and age :Tutor Joes
Your name is : Tutor Joes
Age is : 27
Your name is : Tutor Joes
Age is : 27
To download raw file Click Here

Basic Programs