-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstaticDemo5.java
86 lines (67 loc) · 1.73 KB
/
staticDemo5.java
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
76
77
78
79
80
81
82
83
84
85
86
package Basics;
// https://www.youtube.com/watch?v=U_ho3BRDwsE&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=321
/** static attr can be shared by ALL instances */
public class staticDemo5 {
public static void main(String[] args) {
// run
Circle c1 = new Circle();
Circle c2 = new Circle();
System.out.println("c1.id = " + c1.getId()); // 1001
System.out.println("c2.id = " + c2.getId()); // 1002
System.out.println("total instances = " + Circle.getTotal());
Circle c3 = new Circle();
System.out.println("total instances = " + Circle.getTotal());
Circle c4 = new Circle(9.99);
System.out.println("c4's area = " + c4.findArea());
System.out.println("total instances = " + Circle.getTotal());
}
}
class Circle {
// static attr
private static int total; // how many instances created
private static int init = 1001; // multiple instances share this attr
// attr
private double radius;
private int id;
// constructor
public Circle() {
// first time : 1001
// second time : 1002
// ....
id = init++;
// first time : 0
// second time : 1
// ....
total++;
}
public Circle(double radius) {
// don't forget below
// V1
// this();
// V2
id = id++;
total++;
this.radius = radius;
}
/** Note : getter for total is static method (since total is static attr) */
public static int getTotal() {
return total;
}
public double getRadius() {
return radius;
}
// getter, setter
public void setRadius(double radius) {
this.radius = radius;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
// method
public double findArea() {
return 3.14 * radius * radius;
}
}