KaiquanMah commited on
Commit
3d70db8
·
verified ·
1 Parent(s): dcba1b0

public static dtype method1(param1) {...}

Browse files
Week 6: Methods of OO Programming/01A. Static members [= CLASS MEMBER, not object member] ADDED
@@ -0,0 +1,243 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ We have already seen many examples of 'static' methods.
2
+ In fact, before we started discussing our own classes, all the methods we wrote were 'static'.
3
+
4
+ But what are the static methods?
5
+
6
+ A 'static member' is a 'class member'.
7
+ On the other hand, 'object members' are always connected to an object, meaning that 'we need to create an object to call them'.
8
+
9
+ For example
10
+ Student s = new Student("Simon Student", "02-123456", "99999");
11
+ System.out.println(s.getName());
12
+
13
+
14
+ Hence, 'object member' is connected to one object created out of the class.
15
+ Method getName returns the name of a single student.
16
+
17
+
18
+
19
+
20
+ ========================================
21
+
22
+
23
+ Static methods
24
+ Static method is a 'method of a class'.
25
+ The method declaration includes the keyword 'static'.
26
+
27
+
28
+ When a method is static...
29
+ it can be CALLED WITHOUT CREATING an OBJECT out of the class,
30
+ it is called by using the CLASS NAME, not an object reference, and
31
+ it is not directed towards any object.
32
+ Hence, static methods are often "TOOL METHODS", which get the needed information as arguments.
33
+ They usually return a value, or perform a documented side effect to an object received as an argument.
34
+
35
+
36
+
37
+
38
+
39
+ Let's see an example - a static method called 'emailOK' in class 'Person':
40
+
41
+ class Person {
42
+ private String name;
43
+
44
+ private String email;
45
+
46
+ public Person(String name, String email) {
47
+ this.name = name;
48
+ this.email = email;
49
+ }
50
+
51
+ public String getName() {
52
+ return name;
53
+ }
54
+
55
+ public String getEmail() {
56
+ return email;
57
+ }
58
+
59
+ public static boolean emailOK(String email) {
60
+ if (email.length() < 5) {
61
+ return false;
62
+ }
63
+
64
+ if (!email.contains("@")) {
65
+ return false;
66
+ }
67
+
68
+ if (!email.contains(".")) {
69
+ return false;
70
+ }
71
+ return true;
72
+ }
73
+ }
74
+
75
+
76
+ The method returns 'true', if the email seems to be valid.
77
+
78
+
79
+
80
+
81
+
82
+
83
+ Method can be called from ANY OUTSIDE CLASS using the class name:
84
+ public class Test {
85
+ public static void main(String[] args) {
86
+ System.out.println(Person.emailOK("ernest@example.com"));
87
+
88
+ boolean mailOk = Person.emailOK("address@madeup");
89
+ System.out.println(mailOk);
90
+ }
91
+ }
92
+
93
+
94
+
95
+ As seen above, calling method does not require creating an object - the class name is enough.
96
+ Hence, the client can use the method to validate any email address.
97
+
98
+ Method can naturally be called from the objects created from the class.
99
+ Still, the method is NOW called by using the class name.
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+ Hence, we can add the email validation into constructor:
112
+ class Person {
113
+ private String name;
114
+
115
+ private String email;
116
+
117
+ // CONSTRUCTOR
118
+ public Person(String name, String email) {
119
+ this.name = name;
120
+
121
+ // ADD 'use of static method' here
122
+ if (Person.emailOK(email)) {
123
+ this.email = email;
124
+ } else {
125
+ this.email = "";
126
+ }
127
+ }
128
+
129
+ public String getName() {
130
+ return name;
131
+ }
132
+
133
+ public String getEmail() {
134
+ return email;
135
+ }
136
+
137
+ // STATIC METHOD REMAINS
138
+ public static boolean emailOK(String email) {
139
+ if (email.length() < 5) {
140
+ return false;
141
+ }
142
+
143
+ if (!email.contains("@")) {
144
+ return false;
145
+ }
146
+
147
+ if (!email.contains(".")) {
148
+ return false;
149
+ }
150
+ return true;
151
+ }
152
+ }
153
+
154
+
155
+
156
+
157
+
158
+
159
+
160
+
161
+ Let's look at another example. Class 'Point' has a 'static' method for calculating the distance of a point from the origin:
162
+
163
+ class Point {
164
+ private int x;
165
+ private int y;
166
+
167
+ // constructor
168
+ public Point(int x, int y) {
169
+ this.x = x;
170
+ this.y = y;
171
+ }
172
+
173
+ public int getX() {
174
+ return x;
175
+ }
176
+
177
+ public void setX(int x) {
178
+ this.x = x;
179
+ }
180
+
181
+ public int getY() {
182
+ return y;
183
+ }
184
+
185
+ public void setY(int y) {
186
+ this.y = y;
187
+ }
188
+
189
+ // STATIC METHOD = class member
190
+ public static double distanceFromOrigin(Point point) {
191
+ return Math.sqrt(point.getX() * point.getX() +
192
+ point.getY() * point.getY());
193
+ }
194
+ }
195
+
196
+
197
+
198
+
199
+
200
+
201
+ Again, the static method can be called
202
+ - from another class, or
203
+ - inside object methods in the same class.
204
+
205
+ Let's utilize the 'static' method and write an object method called getDistance.
206
+ Note how the object method class the 'static' method and provides the object itself as an argument (by using the 'this' keyword).
207
+ class Point {
208
+ private int x;
209
+ private int y;
210
+
211
+ public Point(int x, int y) {
212
+ this.x = x;
213
+ this.y = y;
214
+ }
215
+
216
+ public int getX() {
217
+ return x;
218
+ }
219
+
220
+ public void setX(int x) {
221
+ this.x = x;
222
+ }
223
+
224
+ public int getY() {
225
+ return y;
226
+ }
227
+
228
+ public void setY(int y) {
229
+ this.y = y;
230
+ }
231
+
232
+ // OBJECT METHOD calling STATIC METHOD
233
+ public double getDistance() {
234
+ return Point.distanceFromOrigin(this);
235
+ }
236
+
237
+ // STATIC METHOD = STATIC MEMBER = CLASS MEMBER
238
+ public static double distanceFromOrigin(Point point) {
239
+ return Math.sqrt(point.getX() * point.getX() +
240
+ point.getY() * point.getY());
241
+ }
242
+ }
243
+