WEBVTT 1 00:00:00.222 --> 00:00:03.001 In this demo I'm going to show you an example of 2 00:00:03.002 --> 00:00:04.224 calling a method which in turn 3 00:00:04.225 --> 00:00:06.225 calls another method as it runs 4 00:00:07.115 --> 00:00:09.117 I have the methodsdemo project open 5 00:00:09.220 --> 00:00:12.003 and I'm going to use the CodePad for this. 6 00:00:13.118 --> 00:00:16.227 The methodsdemo project has a class MethodsDemo 7 00:00:17.112 --> 00:00:19.111 which just has a couple of methods 8 00:00:19.112 --> 00:00:21.000 method one and method two 9 00:00:21.009 --> 00:00:23.003 lets look at method two first 10 00:00:23.111 --> 00:00:26.007 it doesn't do much at all just prints out a message 11 00:00:26.116 --> 00:00:27.228 in method two. 12 00:00:32.006 --> 00:00:34.113 Lets create and instance of MethodsDemo 13 00:00:34.114 --> 00:00:37.000 I could do this in the object bench or in the CodePad 14 00:00:37.003 --> 00:00:39.223 here I'm going to do it in the CodePad 15 00:00:40.113 --> 00:00:42.003 so lets declare 16 00:00:42.112 -->00:00:45.119 a variable m of type MethodsDemo 17 00:00:46.008 --> 00:00:51.222 and create a new MethodsDemo object 18 00:00:53.004 --> 00:00:56.220 now I can call the method, method two 19 00:00:57.221 --> 00:00:59.222 of that object 20 00:01:01.227 --> 00:01:06.114 and execute that and that just opens a terminal window 21 00:01:07.003 --> 00:01:10.007 and outputs the message that you would expect it to. 22 00:01:12.001 --> 00:01:15.110 I'm going to clear the terminal window because I want 23 00:01:15.111 --> 00:01:18.113 to be sure that the next time I call a method 24 00:01:18.222 --> 00:01:21.225 I'm only seeing the output from that method and not 25 00:01:22.003 --> 00:01:24.112 from the previous method call. 26 00:01:29.227 --> 00:01:31.115 Lets take a look at the other method 27 00:01:31.116 --> 00:01:33.228 in the MethodsDemo class, method one 28 00:01:34.222 --> 00:01:36.006 this prints out a message 29 00:01:36.113 --> 00:01:39.005 and then immediately calls method two 30 00:01:40.009 --> 00:01:43.222 after that it prints out another message 31 00:01:44.119 --> 00:01:48.008 so lets try running that 32 00:01:54.113 --> 00:01:56.221 m dot method one 33 00:01:57.110 --> 00:02:00.003 and again it opens the terminal window 34 00:02:00.005 --> 00:02:02.222 and outputs some messages. 35 00:02:03.110 --> 00:02:05.111 Lets think about whats going on here 36 00:02:05.228 --> 00:02:08.221 the first line of output was produced by 37 00:02:08.226 --> 00:02:12.008 a System dot out dot println statement in method one 38 00:02:13.221 --> 00:02:16.222 the second line of output though is the same as we saw 39 00:02:16.226 --> 00:02:19.229 when I called method two directly 40 00:02:20.222 --> 00:02:23.112 this line is not produced by a statement in method one 41 00:02:23.114 --> 00:02:26.220 it is produced by a System dot out dot println statement 42 00:02:26.225 --> 00:02:28.003 in method two. 43 00:02:29.006 --> 00:02:33.002 This happens because method one called method two 44 00:02:34.001 --> 00:02:37.006 once method two completes then 45 00:02:37.114 --> 00:02:40.117 the code in method one continued and produced 46 00:02:40.222 --> 00:02:43.110 this final line of output. 47 00:02:45.009 --> 00:02:47.228 if I look at the code again 48 00:02:48.229 --> 00:02:50.221 we can see whats happened 49 00:02:51.005 --> 00:02:54.225 I called method one this line executed 50 00:02:55.117 --> 00:02:57.223 then this line, but this line 51 00:02:58.002 --> 00:03:01.009 calls to method two so from here 52 00:03:01.114 --> 00:03:04.110 the execution of the code jumps into method two 53 00:03:04.116 --> 00:03:06.224 and this line of code is executed 54 00:03:06.228 --> 00:03:09.119 and it outputs the message in method two 55 00:03:10.003 --> 00:03:11.223 when this method completes 56 00:03:12.003 --> 00:03:14.119 execution of code jumps back into 57 00:03:14.223 --> 00:03:18.116 method one immediately after the call to method two 58 00:03:18.226 --> 00:03:22.112 and this final line in method one executes. 59 00:03:24.225 --> 00:03:26.112 In an object orientated program 60 00:03:26.114 --> 00:03:28.000 its very common for a method 61 00:03:28.002 --> 00:03:29.227 to call another method 62 00:03:30.006 --> 00:03:33.228 which in turn may even call another method and so on 63 00:03:34.111 --> 00:03:36.112 so its very important to understand the way 64 00:03:36.114 --> 00:03:38.007 these method calls work.