1 00:00:00,360 --> 00:00:00,690 ‫Now. 2 00:00:00,690 --> 00:00:02,400 ‫Did you know the theory of methods? 3 00:00:02,430 --> 00:00:06,240 ‫Let's have a look at the practical aspect of methods. 4 00:00:06,240 --> 00:00:12,960 ‫So let's create a method and we start off by an access modifier and I'm just going to use public. 5 00:00:13,140 --> 00:00:20,130 ‫Then I add the return type, which is void, and then I give a name. 6 00:00:20,130 --> 00:00:24,180 ‫In my case I'm just going to call that write something. 7 00:00:25,590 --> 00:00:28,860 ‫So this method is going to write something. 8 00:00:28,860 --> 00:00:31,970 ‫And first of all, I start like that. 9 00:00:31,990 --> 00:00:39,450 ‫I need curly brackets and in here I simply want to use the console right line. 10 00:00:39,780 --> 00:00:42,660 ‫So I'm just going to write something onto the console. 11 00:00:42,660 --> 00:00:55,260 ‫And this is going to be I am called from a method so pretty simple and then console but read so nothing 12 00:00:55,260 --> 00:01:01,500 ‫too fancy yet and nothing that we haven't seen yet except for this being inside of a method. 13 00:01:01,830 --> 00:01:08,220 ‫So now in order to call this method and to run it, we need to call it within the main method. 14 00:01:08,220 --> 00:01:15,030 ‫So this method here is a method itself and this method is not public, so it doesn't have an access 15 00:01:15,030 --> 00:01:15,810 ‫modifier. 16 00:01:15,810 --> 00:01:23,340 ‫I could even get rid of this access modifier, so I could simply say Void, write something, but we'll 17 00:01:23,340 --> 00:01:26,910 ‫get into the different access modifiers later on. 18 00:01:26,910 --> 00:01:32,790 ‫So this one doesn't have one or main method because it should only be called from this class itself. 19 00:01:32,790 --> 00:01:37,770 ‫Then we have void, which means that this method is never going to return anything. 20 00:01:37,770 --> 00:01:44,340 ‫And then the name is called Main and because well, it's pretty much our main method, it's the, the 21 00:01:44,340 --> 00:01:45,870 ‫one that is really important. 22 00:01:45,870 --> 00:01:54,960 ‫And then within the parentheses you have an array of strings which is called ARX, and that's something 23 00:01:54,960 --> 00:01:56,940 ‫that we will cover later on. 24 00:01:56,940 --> 00:02:02,430 ‫So we haven't seen what arrays are, but we will do so later and you will see what ARGs are as well. 25 00:02:02,700 --> 00:02:08,160 ‫So this is our method and here is our starting point as you have read in the last video or have seen 26 00:02:08,160 --> 00:02:09,060 ‫in the last video. 27 00:02:09,060 --> 00:02:13,410 ‫So whatever we want to run, we need to start it from here. 28 00:02:13,410 --> 00:02:20,700 ‫So let's start and call our write something method. 29 00:02:20,940 --> 00:02:22,650 ‫Now we do get an error here. 30 00:02:22,650 --> 00:02:29,850 ‫We cannot simply call our write something method within the main method because our main method is static. 31 00:02:29,850 --> 00:02:34,980 ‫And that's why the write something method has to be static as well. 32 00:02:34,980 --> 00:02:42,600 ‫So I add the static term and as you can see, it's between the access modifier and the return type. 33 00:02:43,440 --> 00:02:43,830 ‫All right. 34 00:02:43,830 --> 00:02:44,490 ‫So. 35 00:02:45,420 --> 00:02:52,890 ‫Axis modifier, then static, then return type. 36 00:02:54,570 --> 00:02:59,910 ‫And then name of the method or method name. 37 00:03:01,710 --> 00:03:03,060 ‫All right, so that's the order. 38 00:03:03,060 --> 00:03:11,280 ‫And then afterwards, in parentheses, you have parameter one and parameter two, and they have to be 39 00:03:11,280 --> 00:03:12,960 ‫separated by a comma. 40 00:03:13,380 --> 00:03:14,010 ‫All right. 41 00:03:14,010 --> 00:03:17,280 ‫So that's the usual structure and that's what we have here. 42 00:03:17,280 --> 00:03:20,240 ‫So we have a public, which is the excess modifier. 43 00:03:20,250 --> 00:03:25,320 ‫We have static, which is used because the main method is static. 44 00:03:25,320 --> 00:03:31,800 ‫And static simply means that this method is static and declares a static member which belongs to the 45 00:03:31,800 --> 00:03:34,560 ‫type itself rather than to a specific object. 46 00:03:34,600 --> 00:03:39,660 ‫That's also something that will make a lot more sense as soon as we get to the point where we use object 47 00:03:39,660 --> 00:03:41,340 ‫oriented programming. 48 00:03:41,340 --> 00:03:47,910 ‫So for now, just know that whenever you want to call a method within a static method, you need to 49 00:03:47,910 --> 00:03:50,430 ‫make the method itself static as well. 50 00:03:50,640 --> 00:03:56,820 ‫So for now, as we don't use object oriented programming and the whole, the bigger concepts would rather 51 00:03:56,820 --> 00:03:58,620 ‫be difficult to understand. 52 00:03:59,160 --> 00:04:00,570 ‫Just let's just keep it at that. 53 00:04:00,570 --> 00:04:06,750 ‫Because static is required here, because we want to call it from another static method. 54 00:04:06,900 --> 00:04:07,400 ‫All right. 55 00:04:07,410 --> 00:04:12,120 ‫And later on, if we have multiple classes, that will not be required anymore, and then we'll have 56 00:04:12,120 --> 00:04:13,770 ‫a look at that as well. 57 00:04:13,830 --> 00:04:15,500 ‫So here's our result. 58 00:04:15,510 --> 00:04:17,610 ‫I am called from a method. 59 00:04:17,610 --> 00:04:21,390 ‫So as you can see, we didn't have to write it within our main method. 60 00:04:21,390 --> 00:04:23,330 ‫We simply wrote it in here. 61 00:04:23,340 --> 00:04:29,880 ‫So you can create very long methods which do loads of things, and then you can simply call them in 62 00:04:29,880 --> 00:04:30,780 ‫one line. 63 00:04:30,780 --> 00:04:34,890 ‫So this is how we call them and that's how we declare or create them. 64 00:04:35,370 --> 00:04:37,890 ‫And a method should usually do one thing. 65 00:04:37,890 --> 00:04:41,850 ‫So in this case, this method is just there to write something. 66 00:04:41,850 --> 00:04:45,740 ‫So you shouldn't create methods which do 20 different things or something like that. 67 00:04:45,750 --> 00:04:48,270 ‫It's best if you keep it simple. 68 00:04:48,270 --> 00:04:53,520 ‫So if you want to have a method which does something else, then create an additional method. 69 00:04:53,520 --> 00:04:55,110 ‫So a separate method. 70 00:04:55,650 --> 00:05:03,450 ‫All right, so let's create an additional method which doesn't just write something what we have here, 71 00:05:03,450 --> 00:05:07,490 ‫but which writes something that we want to have. 72 00:05:07,500 --> 00:05:15,480 ‫So public static void, write something specific. 73 00:05:16,590 --> 00:05:20,630 ‫So our first example, it just wrote anything. 74 00:05:20,640 --> 00:05:23,100 ‫Well, whatever was written in here. 75 00:05:23,100 --> 00:05:30,570 ‫But now what I want to have, I want to declare what I want to write whenever I call this method. 76 00:05:30,990 --> 00:05:32,820 ‫So let's have a look at that. 77 00:05:33,120 --> 00:05:38,400 ‫So I'm going to use console right line and now what should I write here? 78 00:05:38,760 --> 00:05:42,090 ‫So I don't want to write anything. 79 00:05:42,090 --> 00:05:44,310 ‫I want to write something specific. 80 00:05:44,310 --> 00:05:47,730 ‫And what that is, is what I'm going to add as a parameter. 81 00:05:47,730 --> 00:05:55,860 ‫So here I have parameter one and parameter two, and in here I want to have only one parameter, which 82 00:05:55,860 --> 00:05:58,500 ‫is going to be the text of type string. 83 00:05:58,500 --> 00:06:07,590 ‫So my text is going to be the parameter and whenever I want to call this method, I can now hand over 84 00:06:07,590 --> 00:06:11,010 ‫my text and it will be printed onto the console. 85 00:06:11,010 --> 00:06:18,000 ‫So I deliver my text here and now it will write to the console. 86 00:06:18,000 --> 00:06:24,600 ‫So let's create read as well or call the read method here as well in order to write it onto the console. 87 00:06:24,600 --> 00:06:28,830 ‫And now let's call this write something specific method. 88 00:06:29,190 --> 00:06:34,650 ‫And if we just call it like that, we get an error because it says there is no argument given that corresponds 89 00:06:34,650 --> 00:06:38,010 ‫to the required formal power parameter. 90 00:06:38,010 --> 00:06:39,000 ‫My text. 91 00:06:39,000 --> 00:06:41,370 ‫So that means we need to give an argument. 92 00:06:41,370 --> 00:06:41,730 ‫All right. 93 00:06:41,730 --> 00:06:45,630 ‫So when you create a method, this is called parameter. 94 00:06:45,690 --> 00:06:49,800 ‫But when you call the method, what you hand over is the argument. 95 00:06:49,800 --> 00:06:52,470 ‫So I'm going to hand over the argument. 96 00:06:52,560 --> 00:07:00,690 ‫I am an argument and am called from a method. 97 00:07:01,500 --> 00:07:08,610 ‫And here, as you can see, I use exclamation marks as well because this is of type string. 98 00:07:08,610 --> 00:07:13,350 ‫So my text is of type string, so I have to hand over a string here. 99 00:07:13,350 --> 00:07:14,940 ‫So let's run that as well. 100 00:07:15,060 --> 00:07:22,620 ‫And you might wonder I am called from a method is shown, but I am an argument and I'm called from a 101 00:07:22,620 --> 00:07:25,140 ‫method is not written onto our console. 102 00:07:25,170 --> 00:07:26,180 ‫Why is that. 103 00:07:26,190 --> 00:07:28,110 ‫That has to do with this, right? 104 00:07:28,110 --> 00:07:34,170 ‫Because console read reads out whatever is written by that point and it doesn't read whatever is written 105 00:07:34,170 --> 00:07:34,800 ‫afterwards. 106 00:07:34,800 --> 00:07:42,660 ‫So what we can do is get rid of that console right from here and simply call it in here. 107 00:07:42,660 --> 00:07:42,930 ‫So. 108 00:07:43,050 --> 00:07:44,910 ‫Console dot read. 109 00:07:46,200 --> 00:07:47,570 ‫And now let's try it again. 110 00:07:49,910 --> 00:07:56,570 ‫And as you can see, I'm called from a method and I am another argument and I'm called from a method. 111 00:07:56,960 --> 00:08:03,360 ‫So you have seen how to use arguments and parameters and create methods and use void. 112 00:08:03,380 --> 00:08:06,680 ‫And we're going to have a look at other types as well. 113 00:08:06,680 --> 00:08:10,370 ‫So in the next video, we're going to have a look at different return types. 114 00:08:10,370 --> 00:08:14,150 ‫And then we're also going to use more than just one parameter.