-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyConstructor.java
More file actions
86 lines (55 loc) · 1.43 KB
/
CopyConstructor.java
File metadata and controls
86 lines (55 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// http://www.javatpoint.com/constructor
/*
There is no copy constructor in java. But, we can copy the values of one object to
another like copy constructor in C++.
There are many ways to copy the values of one object into another in java. They are:
By constructor
By assigning the values of one object into another
By clone() method of Object class
*/
class CopyConstructor {
int id;
String name;
CopyConstructor (int i, String n){
id = i;
name = n;
}
CopyConstructor (CopyConstructor s){
id = s.id;
name = s.name;
}
void display (){
System.out.println("id : " + id + ", Name: " + name);
}
public static void main (String args[]){
CopyConstructor cc1 = new CopyConstructor(111, "AMRAN");
CopyConstructor cc2 = new CopyConstructor(cc1);
cc1.display();
cc2.display();
}
}
/*
Method 2:
We can copy the values of one object into another by assigning the objects values to
another object. In this case, there is no need to create the constructor.
class Student7{
int id;
String name;
Student7(int i,String n){
id = i;
name = n;
}
Student7(){}
void display(){
System.out.println(id+" "+name);
}
public static void main(String args[]){
Student7 s1 = new Student7(111,"Karan");
Student7 s2 = new Student7();
s2.id=s1.id;
s2.name=s1.name;
s1.display(); // 111 Karan
s2.display(); // 111 Karan
}
}
*/