 Welcome to Intro to Java with an emphasis on AP Computer Science A with Tokyo EdTech that is me in today's video We're gonna be taking a look at static methods. So static methods We're gonna look at how to define a static method. We're gonna look at how to call static methods We're gonna be sending values to methods where we returning values to methods We're gonna take a look at something called scope and local variables This kind of tells us where these variables are accessible and look at a very important concept called method Overloading so let's go ahead and get started. Let's take a look at the code So first of all the question is you know, what is a method? So a method is just you know and organize like I think it's like a section of code that does a particular task So oftentimes we have repeated coding in our program and we always want to report We always want to avoid repeated code So what we do is we take that code and we put it into a method and then we call that method So let's go ahead and just go ahead and write a simple method. You can see here It says send no value return no value. We'll talk about that in a minute So the first thing when you know about a method is it public or private? So public means it is accessible from outside of this Class from another class or is it only except if it's private It's only accessible within that class. So far in this case, we're like make sure it's public But it would work either way to be perfectly honest Static and static means that it is part of the class. So We have this class called static methods So if we were to use this method outside of this class It would be static methods dot and one of we're about to call this So for now, this is a static method. Keep in mind static methods I like to say are attached to classes and then we need to return type in this case We are returning no value. So it is void And again, we saw some of this stuff in the earlier unit especially with strings and string methods and the math class and the math class methods and This is going to be called print the answer So this is a very simple method system dot out dot print L n and it prints 42 because that is the answer the ultimate answer to life the universe and everything So if I compile this It compiles and if I run it We see basically nothing. There's no output here So that's because we need to actually call the method. So I'm gonna say print the answer And we have to compile it again Run it and I can see that it is it actually outputs 42 which is quite nice for us So how this works is if you recall our execution begins with the main method So we get the main method and we get to print the answer So it looks for this method and says, oh, there's a method called print the answer. It jumps down here Executes this code And then when it gets back to the end it jumps back and continues now There's no other code in here. So we don't see anything else, but that's basically how it works Nice, that's pretty pretty straightforward. I lost to explain it Now this is a very simple method Again, we are not sending a value This is empty and we are not returning a value because this is void. Okay, so let's go ahead and send a value and Not return a value. So I'm gonna go ahead and make a new method and we'll call it public static Remember, we're doing static methods here void and if you remember in our loop unit, we did it count down And I go int seconds I'll explain all this in a minute and hopefully this will look familiar. So for It was int i equals seconds That here i is greater than zero i Minus minus because we're counting down. Yeah, this doesn't look familiar rewatch the iteration video and system dot system dot out that print Ln, we're gonna print I And then once that is done once the loop is done, we're gonna go ahead and print system That out that print Ln We're gonna close that off. I notice here. There's a little problem. Let's see here. There we go So this should match that and this should match that So again, I want to compile it Compile that's a good sign, but we also need to call it So I'm gonna go ahead and say say count Down So what happens is this value 10? Is sent down to here The seconds so that's the same as writing That'd be the same as writing like in here seconds equals 10 But what's nice about this is it's not Uh, it's not Stuck at 10 if we wanted to have a longer account that we can make it 20 we can make it Nobody nobody cares. Uh, whatever number we want to put in there. We can put in there So it jumps down to here Seconds becomes in this case 10 So i equals seconds. So i equals 10 i is greater than zero. So we print 10 minus 1 9 8 7 6 5 4 3 2 1 And then we get to zero zero is not greater than zero Loop ends and then we print boom. So let's go ahead and compile it. I think I already did that and run it You can see there. We've got 10 9 8 7 6 5 4 3 2 1 and boom So again, what's nice about this is if later in our program Let's say, okay. Well now we need to do a 30 second countdown So instead of having to Copy and paste this change this to from 10 to 30. I can just do this over and over again No matter how many seconds I want to count down from So you can see here. We got 30 All the way down to boom Pretty pretty cool So that was a case where we are Sending a value. Well the first case was Sending no value returning no value second case was sending a value now. We can send multiple values I just happened to send one here But you get the idea and in this case, we're going to send a value and return a value And again, we've seen this in the math class We did math dots where root for example We sent, you know 25.0 and it returned 5.0 for example. So let's go ahead and Should be public static double I'll say get double we'll say int x and I could call this value doesn't really matter And then what we do is we return the value I would say x times 2.0 Um And what I should have done here was this should be a double. I guess we could do an int. Let's try it Let's make that an int We send an int and we'll return a double So you notice I multiplied it by 2.0 here and uh Yeah, let's go ahead and do it that way. Um, so So I can go ahead and Do uh get double and let's say five So I'm gonna go ahead and compile that And execute it You notice here. We don't see anything. Okay, so this is this is a common beginner mistake I did this intentionally believe it or not. Um, so The return value Has to go somewhere Okay, so we sent this int in this case five We're returning the double should be 10.0 But we returned it and did nothing with it So we either have to do we can do system dot out dot print l n And then there we go. We've printed that value out. So this the value is returned This gets replaced with 10.0 in this case And we are good to go now. We could also go double. Um, so you call it, uh, double val value equals get double five And then yeah, we could separate out like that if you preferred value But this double needs to match the return type And then same thing. So we get to here Five is sent to here We multiply five by 2.0 We return that value back up to here It's assigned to this value variable and then we print it out the variable And yeah, that's pretty much it. Um, so we've got One more case where we send no value but return a value Um, so this is not super common, but it is possible. So I'll just show you real quick So I could do public static Um, we return it by let's go ahead and return um That's sort of call it, uh, pie. Okay, and So we return I'll say 3.14159 And I missed a step there. I forgot to put the return type, which I often do So we want to return because the value pie is approximately 3.14159 So I can go ahead here and do system dot out dot print l n uh pie that's parentheses And if I Compile it and run it We see 3.14159 again. So here we're not sending a value This is empty, but we are returning a double in this case. We went ahead and printed the double directly Okay So, uh, our next our next, uh, that's about that's about all you need to know about methods We can send values return values. Everything's got to match up. So just be real careful with that um, so scope Um, scope talks about when and where we can access a variable So for example, we could put a variable up here public static Uh, say, you know, let's say x equals 5 for example And then you see down here we we actually have This x Just kind of interesting Um, now since they're the same value, let's go ahead and make this Get double makes like this 10 and see what happens so we can pile it Up Oh, I forgot the type Let's do that a lot And you'll see now It was 10 times 2.0 or which gave us 20 So you see here we have this weird thing. We have an x here And we also have an x up here So let me go ahead and Try something I'm gonna go ahead and delete this x I'm gonna go ahead to get double So now we're not sending a value so we can compile it And you can see it gave us 10.0. So what happened? So here's the trick, uh, so when we get to this method You see this x So the first thing it does is it looks locally Inside the method And you'll see here Is there an x in here and the answer is no So the next thing it looks is into the class itself. Does the class have An x somewhere and actually it does because we made a public static int called x equals five and it uses this value Pretty cool. So let me go ahead and do 10.0 Or 10 again Let me put back int x And let me go ahead and do some printing here. So I'm gonna go ahead and take Take this again. Go ahead and say So system dot out dot print l n x Go ahead and copy that. So let's compile that Right And let's see what happens here. Okay. So you see we've got five 20 and five So what happened is this x it first looked Here in this method And there was no x so then it looked outside into the class and found an x and that's what's going to use But this 10 gets sent down to here Okay, this x is different to this x So the scope tells you where it is available So in this case, this is the local x because we have an x declared here And the reason is if we wanted to take this method and use it in another program We could do that We don't have to worry about the fact that there is already a variable called x Out in this part of the program I guess that gives us a little bit of flexibility and the ability to reuse code better. So Doing this this x has no impact on this x Which is pretty cool On a side note Oops, I should probably put that down here in this section. So if I do this for int i equals zero i is less than 10 i plus plus And do system dot out dot print l n i And I do system and I spawn around system dot out dot print l n i If I compile this We're gonna see an error You'll see here static methods Tells you line 25 It says cannot find symbol Okay, it actually points to the i so you're it tells you exactly what the problem is Okay, I'm gonna really learn to read these error methods error error messages So you can see this i it even though it's Declared here It doesn't know what it is here. Okay, that is because These variables that are declared in the four statement declaration are local To this loop So they only exist from here to here once this is done It's over Pretty cool. Now that said if I have done this Int i equals zero Compiled it I'll get an error because it says This i Is already declared here. So the rules are a little bit weird. So in this case, I would have to do this Because I already declared it out there Pretty nifty pretty nifty Again, it's just something to keep track of and be aware of you may see these Types of errors and that tells you kind of where different variables can be accessed And our last uh, our last, uh topic Is overloading So To overload a variable Um Or to overload a method, uh, so An overloaded method has Same name Different signature And signature Is the number type And order Of the parameters In the method declaration Okay, so let me go ahead and explain that So Oops I'm going to make a new method public static avoid And it's going to be print greeting I'm going to say system Dot out dot print l n Eight what's going on Then I'm going to make another function another method what I'm going to put in there Is I'm going to put string message In this case I'm going to go ahead and print Plus message So now Watch what I do here So I'm going to say print greeting And print greeting I'm going to compile that and I still have some of that problem with the audience down here Gotta get rid of that So you can see here We have two methods So this part is exactly the same But this part is different So in this case there is nothing being sent So the signature is empty In this case a string is being sent So that's the signature So this is empty And this is this would be one string So what happens is When we try to call the method It says okay, is there a method called print greeting that takes no arguments takes no values And it calls that method If there is a method and then in this case, okay, here's a string Is there a method with that name? That accepts one string And in this case there it is So you can so based on the type and the number of The data that's being sent To the method It chooses which one to call and that is what we refer to as overloading Okay, so same method Different signature Pretty cool. So that's about it. That's that's kind of the gist of static methods You know my ebook which is linked, you know in the description has a bit more information about that and some more examples You might find helpful But uh, yeah, go ahead and give it give that a look and hopefully this will help you get started And will help you to keep on coding. So again, we learned how to define some methods We learned how to call some methods sending values sometimes not sending values other times Returning values sometimes not returning values other times We looked at the idea of scope and idea of a local variable And so, you know, how that works and why we might use that for code reusability Then we looked at the very important concept of method overloading So if you have any questions put them in the comments. Otherwise, uh, keep on coding. Take care