 Hey everybody, this is Brian and this is our seventh Java video today. We're gonna be discussing methods What is a method? Well, this is a method right here this public static void main a Method is a block of code. For example, we're gonna say public whoops public void test And brackets what does this do? Remember public is our access modifier. So this is available to the public void means it returns nothing Test is the name and then our parameter list We're just going to say system out Dot Print line Hello from test and run it Nothing happens. Why hmm? We're not using the method to use the method Type the method name with the parameter list Uh-oh cannot make a static reference to a non static method test from type start. What does that mean? Well, if you remember static Means that the class owns us where public is Inaccess modifier. Are those two related? No, they're both public Static once again means this belongs to the class This is not static Meaning we'd have to create an instance of the start class So let's do that Now notice syntax is a little different than what we've seen in our variable tutorials We're saying start and then the name. So this is the class. This is the name Equals new start so we're creating a new instance of this blueprint or this class called start and Then we're going to say m start dot And there's our test function now Notice that this is case sensitive meaning if you do test that It tries to change it to uppercase and eclipse some older ones won't do it and you're gonna get an error because it's lower case It's not the same as uppercase So what happens we run this now? Hello from test so because this is not static We have to create an instance of it What that means is? Static is created when the class is loaded into memory When you create an instance of that class you need a non-static reference Let's kind of drill that in here We'll say a public Static whoops void do test Actually, let's call this static Static test. We'll say hello from static test now Let's just comment these out And we're going to say start dot And see how there is a static test We've created we have not created an instance of this class yet We can access the static methods because the class is loaded into memory Let's run it to verify. Yep. Hello from static test So what you need to understand is that? Static methods are loaded with the class Non-static methods you need an instance of that class or you need an object to work with Now let's Back up here. Let's make something a little different. Let's say Public int We'll call this 10 years We'll say int Age We're just going to make something that returns your age plus 10 years Now you notice how this is complaining saying this method must return result type int. What does that mean? Well? Remember void returns nothing int Void int. Okay, so we have to return an integer Why they didn't say public static null. I don't know it seems like it would have been a better convention It's probably a holdover from the C++ days. Just know that we have to return an integer. So we'll say age plus 10 So when we call 10 years, we have to provide an argument called an age And it's going to return the age plus 10 years So let's do that. Let's say Start dot 10 years 36 and If we run this, what do you think's going to happen? Well, nothing's gonna happen Why because we're not printing anything out into the console and let's just take our object here And I'd really advise you get familiar with copying and pasting it'll save you a lot of typing and You see how we're saying system out print line and then we're saying our object start. I'm sorry and start We're calling the 10 years method We're giving it 36 and it spit out 46 So that's how methods with arguments work Now methods can have multiple arguments and to do that You just do a comma and then the other argument now an argument is simply a type and a name So we have end-age string name Now this is also called Operator overloading notice how we have two Methods called 10 years. Oops But we have two different argument lists to overload a method You need to have a different argument list Once again, these are two different methods with the same name different argument less Now When you go to do this You see how there are two 10 years in your list. Let's actually move this down here. So you can see what's going on One calls age one calls age and name so you can call either one Now, what do you think's gonna happen when we run that code? Which one of these is it gonna call we've got a 36 and a name Is it gonna call this one? No It's gonna call this one so a lot of times what you can do is Call the other one Saves you from Redoing all your code. Let's say this is hundreds and hundreds of lines of code. Well, you don't want to redo that You just want to add a little extra functionality here. So we're just gonna print the name and then return the age Let's run this 46 Brian 46 so you see it worked We're calling This method right here 10 years with two arguments Spitting out the name and returning this method up here, which is age plus 10 Now if this was a little confusing I understand This is a little daunting walking into it the first time if you've worked with other programming languages This was probably not a problem. So thank you for watching. I hope you found this video educational entertaining and I look forward to your feedback