-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThisUse2.java
More file actions
60 lines (54 loc) · 1.12 KB
/
Copy pathThisUse2.java
File metadata and controls
60 lines (54 loc) · 1.12 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
class Student{
String name;
int age;
public void printinfo()
{
System.out.println("info are:");
System.out.println(this.name);
System.out.println(this.age);
}
Student()
{
System.out.println("non parametrized constructor called");
}
Student(String name,int age)
{
this.name=name;
this.age=age;
System.out.println("parametrized constructor called");
System.out.println(this.name);
System.out.println(this.age);
}
Student(Student s2)
{
System.out.println("copy constructor");
this.name=s2.name;
this.age=s2.age;
}
}
public class ThisUse2{
public static void main(String []args)
{
Student s1=new Student();
s1.name="jack";
s1.age=22;
s1.printinfo();
Student s2=new Student("frost",23);
Student s3=new Student(s2);//copy constructor..assign objct into another object..used to make copy of object
s3.printinfo();//no need to write destructor..automatically..garbage collector
}
}
/*
output....
non parametrized constructor called
info are:
jack
22
parametrized constructor called
frost
23
copy constructor
info are:
frost
23
*/