File size: 7,078 Bytes
33852e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
1
00:00:00,000 --> 00:00:01,000
Hello team!

2
00:00:01,000 --> 00:00:04,000
In this section we will learn with you more about methods in Java.

3
00:00:04,000 --> 00:00:09,000
We are going to have multiple lessons in this section from which you will learn what is method and what

4
00:00:09,000 --> 00:00:13,000
is method signature, how to declare methods and how to use them.

5
00:00:13,000 --> 00:00:16,000
You will understand how methods can return values.

6
00:00:16,000 --> 00:00:20,000
I will explain you how data is passed in Java by value and not by reference.

7
00:00:20,000 --> 00:00:24,000
How to write recursive methods and what are variable length arguments?

8
00:00:25,000 --> 00:00:26,000
Let's start.

9
00:00:26,000 --> 00:00:27,000
What is method?

10
00:00:28,000 --> 00:00:33,000
A method is a block of code which is referred to by name, and can be called at any point in your program

11
00:00:33,000 --> 00:00:35,000
simply with the methods name.

12
00:00:36,000 --> 00:00:40,000
Think of a method as a chunk of code, which can be performed on the request.

13
00:00:40,000 --> 00:00:45,000
When during the program execution method call is performed, the program proceed execution from that

14
00:00:45,000 --> 00:00:51,000
method and when method is finished, execution returns to the line of code in your program from where

15
00:00:51,000 --> 00:00:52,000
method was called.

16
00:00:53,000 --> 00:00:55,000
Let's write method together.

17
00:00:55,000 --> 00:00:58,000
Each method has access modifier.

18
00:00:58,000 --> 00:01:01,000
We will learn them during the object oriented programming topic.

19
00:01:02,000 --> 00:01:03,000
Let's write private.

20
00:01:03,000 --> 00:01:09,000
As of now, this means that we can access this method only in scope of this class.

21
00:01:10,000 --> 00:01:12,000
After that, let's write static.

22
00:01:12,000 --> 00:01:18,000
Static keyword means that this method is connected not with instances of this class, but with class

23
00:01:18,000 --> 00:01:19,000
itself.

24
00:01:19,000 --> 00:01:24,000
We'll have separate lessons where we will take a look at what a static and non-static methods.

25
00:01:24,000 --> 00:01:26,000
While we don't know OOP.

26
00:01:26,000 --> 00:01:28,000
Let's create static methods.

27
00:01:29,000 --> 00:01:31,000
Here we specify method return type.

28
00:01:31,000 --> 00:01:34,000
Each method can return some value after it finished.

29
00:01:35,000 --> 00:01:40,000
The result of the method execution can be assigned to variable of the same type as return type.

30
00:01:40,000 --> 00:01:42,000
And here is method name.

31
00:01:42,000 --> 00:01:48,000
Method names are usually verbs started with lowercase letter and written in camel case like this one,

32
00:01:48,000 --> 00:01:49,000
for example.

33
00:01:50,000 --> 00:01:52,000
It is very important how you name your methods.

34
00:01:53,000 --> 00:01:55,000
For example, guess what does this method do?

35
00:01:56,000 --> 00:01:58,000
It is straight and clear what this method does.

36
00:01:58,000 --> 00:02:02,000
You can save a lot of time for your colleagues and a lot of money for your company.

37
00:02:02,000 --> 00:02:06,000
Who pays for time of your colleagues if you will write clean code?

38
00:02:07,000 --> 00:02:13,000
Understanding of code which was written not by you can take up to 50% of time dedicated for development.

39
00:02:14,000 --> 00:02:19,000
That's why in this and in future lessons, I will put stress on clean code design.

40
00:02:19,000 --> 00:02:24,000
Because clean code is something that you can't learn in one day after you learned programming.

41
00:02:24,000 --> 00:02:27,000
It is something that you should learn constantly.

42
00:02:28,000 --> 00:02:31,000
After we wrote Methodname, we need to specify method parameters.

43
00:02:31,000 --> 00:02:37,000
Here we declare that this method can work with two integers, and int values can be passed as arguments

44
00:02:37,000 --> 00:02:38,000
to this method.

45
00:02:39,000 --> 00:02:43,000
Method name and method parameters together is called method signature.

46
00:02:44,000 --> 00:02:47,000
Why return type or access modifier is not part of method signature?

47
00:02:48,000 --> 00:02:53,000
The reason for this emphasis on just the method name and parameter list is because of overloading.

48
00:02:54,000 --> 00:02:55,000
What is overloading?

49
00:02:56,000 --> 00:03:02,000
Overloading it is the ability to write methods that have the same name but accept different arguments.

50
00:03:02,000 --> 00:03:08,000
The Java compiler is able to discern the difference between the methods through their method signatures.

51
00:03:08,000 --> 00:03:12,000
For example, we have method sum which can sum two arguments.

52
00:03:12,000 --> 00:03:17,000
This method can sum two integers and this method can sum to doubles.

53
00:03:17,000 --> 00:03:23,000
We can say that method sum is overloaded because we have two methods with the same name, but with different

54
00:03:23,000 --> 00:03:24,000
signatures.

55
00:03:24,000 --> 00:03:27,000
After we declared parameters, we should put braces.

56
00:03:27,000 --> 00:03:32,000
This will be our method body which will be executed each time we will call method.

57
00:03:32,000 --> 00:03:36,000
If method has return type, we should return some value from the method.

58
00:03:36,000 --> 00:03:42,000
To return value from the method, we need to write return keyword and specify either variable name or

59
00:03:42,000 --> 00:03:47,000
expression which has compatible type with the one specified in method declaration.

60
00:03:47,000 --> 00:03:50,000
It can be that method returns nothing.

61
00:03:50,000 --> 00:03:54,000
For example, here are methods which just prints values to console.

62
00:03:54,000 --> 00:03:58,000
They should return nothing because they just print values to console.

63
00:03:59,000 --> 00:04:03,000
In this case you should specify void as return type like this.

64
00:04:03,000 --> 00:04:10,000
And here you can find examples of the method invocations I call method some and it returns value.

65
00:04:10,000 --> 00:04:15,000
So I can initialize this int variable with the value which was returned from this method.

66
00:04:15,000 --> 00:04:17,000
Is it clear?

67
00:04:17,000 --> 00:04:22,000
I can call method and can pass to it the result of execution of another method like this.

68
00:04:23,000 --> 00:04:30,000
That means that this method will be executed first, and after that this method will be executed with

69
00:04:30,000 --> 00:04:32,000
value, which was returned from some method.

70
00:04:33,000 --> 00:04:38,000
You can find that printer console is also overloaded method which works with different types string,

71
00:04:38,000 --> 00:04:40,000
double and int.

72
00:04:40,000 --> 00:04:41,000
That's all.

73
00:04:41,000 --> 00:04:43,000
Thanks a lot for your attention.

74
00:04:43,000 --> 00:04:45,000
See you in the next lesson.

75
00:04:45,000 --> 00:04:46,000
Us.