 Okay, welcome to Intro to AP Computer Science for new AP teachers. In this video, I'm going to talk about static methods, and they are obviously very important. So first topic is what is a static method, talk about the basic structure, making sure the students understand the difference between what a method call is versus a method definition. Now they've seen method calls before in the strings unit, or also in the math class, math dot square roots, string dot life, for example. But now we're going to actually be writing our own methods and definitions. And again, they should be somewhat familiar with the idea of sending values and returning values from what I just mentioned. But here we really need to hammer it home and to show how all the different little pieces of the puzzle fit together and how, you know, what you send has to match with what you return. Talking about the idea of a signature, because there are quite a few, not quite a few, but you'll see questions on the AP exam, especially in the multiple choice. And I'll say, well, which of these methods is possible based on the signature, you have to understand, you know, how signatures work. And then overloading, which is, of course, multiple, you know, same function name, different signature, which is what I was just talking about, then have an idea for an introductory exercise that you can use with students. So what is a static method? So a static method is a method that belongs to a class as opposed to an object. So this, it's a bit of a hard concept to explain, just because, you know, classes are a bit more abstract. But, you know, as you know, like, so for example, let's say I have a student class, and one of the examples I'll use is what we can have a static attribute, like number of students. And so we look at a student, student has an age, student has a grade, but the number of students is not related to that student personally. That's, you know, that student adds to that attribute in this particular case, but it's not an attribute of that student of an individual student. It's an attribute of them as a collection, as a collective. So that's kind of how I kind of think about it is it belongs to the class, not individual. So it's just one thing, not multiple. So again, it's a little ephemeral, that one. Again, it's just static methods are a way of organizing code to make it reusable, and to make it just easier to update and to maintain. And typically, well, a static method is either called directly from within the same class or called using dot notation from within another class. And again, we've seen this, we've seen math dot square root, for example, would be an example of a static method that belongs to the class, in this case, math. The basic structure, again, we see public, static, void, and not always void, but public static, and which is the return type, the name of the method, and then whatever arguments go to that method. So we have a choice between public and private, we have a choice between, well, in this case, it's static or not static, if it's not static, it's not a static method, go figure. And the return type is what type of value does it return void means it doesn't return type. Then we have our method name, which again follows the same naming conventions as what camel case in this case, then whatever the arguments are, these are the values that we are sending that we can send to that particular method. So the method call versus the method definition. So we have to first define the method. What's nice about Java is it doesn't matter if you define it afterwards or define it before, because in the compilation process from source code, the byte code, it just kind of, you know, fixes all that for you. Other languages that order is a bit more important. So the method definition goes outside of the main method. So that's important. So make sure that, you know, the students closed the main method and then the next, you know, these static methods come afterwards. Typically, I put it afterwards. And the method call comes in inside another method, usually the main method, but a static method can call a static method, which could call another static method. This is not uncommon. But for most of the programs that we do in AP computer science, typically, you're calling from inside the main method, although you will see a few, you know, exceptions to that. Sending values. So we can send values to methods. And as I mentioned, we've seen this before with math dot square root. So in this case, we're sending the value 25.0 to the static method sqrt. And the number and type of value sent must match what you see in the definition. So if we were to look at the dot square root method definition in Java, so I don't know if we could find that, we would see in there double something because it accepts a double returning values. We have a choice whether or not to return a value. We can also return objects. I should have mentioned that we can also send objects. But at this point, the only type of object they really know is a, well, I guess now we did the person object. So, but typically speaking, you're sending values and then later you start sending a bit more object type stuff. And technically it's an object reference, which is something that should be brought up either sooner or later. So you can see in this example, double x equals math dot square root 25.0. So the value that we're sending is 25.0. And we're returning, of course, it'd be 5.0, which would then be assigned to x, which is also a double. In Java, only one value or object can be returned. So that could be a collection though. But like for example, Python, you can return multiple values, which is kind of cool. But yeah, Java, you can't do that. So your Python students, they're coming from Python might be like, hey, why can't we do this? Because that's just the way it is. The signature. You'll see the concept of the signature. And again, it's important because there are multiple choice questions on the AP exam, where this isn't this is important. So let's say you have an example here, you see on the screen at the bottom, you have a string string int. And then the trying to send a string and int string, it's not going to work. So the signature refers to the number type and order of values in the method definition. So this example, public static double double x, a lot of doubles there. So the signature here would just be a single double. But yeah, you could have a signature like so first, last, first name, last name, age. So in this case, string string int. And what I tell my students to do, when they're taking the test is just a little bit of a shortcut is to write the signature like this. So capital S, capital S, and I. So that'll kind of usually that'll, you know, they're not going to have like some other, you know, object with the same capital letter, typically speaking. So I for integer D for double and B for Boolean, capital S for string. And if there's another type, they would use a capital capital letter for that. That way they can just say, okay, this is a SSI, but this one is an SIS. I can't use that. Overloading. This is important concept as well. It's kind of related to what we just saw. So if you have a signature, you know, so it's two methods with the same name, but different signatures. So based on the values that are sent, each, the method that's called is changes. So for example, you know, public static, double it, public static, double it, double x. So you can see here, we have an int as our signature. And here we have a double. So which method is called depends on the type of value sent. So if I send an integer, it's going to call this method. If I send a double, it's going to call this method. You can do a lot of interesting stuff with that, especially with constructors when you get to that later. And for my introductory exercise, this is what I actually tend to do before I explain all these things. Or we can do it after as well. So what I do is I have four, four students as volunteers. I have them stand, you know, spread out in front of the board and above each student, I write, you know, void print 42, and get the answer void print double, you know, so each student represents one of these methods. And then I'll take some paper and or many white board effect on these days when I have one, and write a number on it. And I give it to a student, I say, okay, you know, you choose which method to call. But if it takes, for example, in this case, if you call this one, then you have to, you have to actually physically take the white board over, or physically take that piece of paper over and give it to that student, and then go sit back down. Now, in the case where there's a return type, that student at the board has to then either take a little white board or a piece of paper and walk it over physically to that other student. Now, you could just do, you know, take a tennis ball and say, they throw it, hey, here's a four. And the person catches it, hey, here's an eight. You know, so they just really visualize that process of passing values around the program, you know, at least in the real world. So moving those numbers or tennis ball, whatever adaptation you want to do, it could be a little, you know, a little frisbee or something, depending on how safe your students are. My students are pretty safe. So a tennis ball works or even a ping pong ball, if you're worried about injuries. So that way, you know, that you're sending values back and forth. And they're, they're visually seeing it and also a little bit of physical activity. It's nice to get the students out of their chairs when you can, especially in a coding course. That's how I, that's how I introduce these concepts with the students and practice them. So topics today were what is a static method? We looked at the basic structure, and again, make sure to emphasize the difference between the method call and the method definition, sending and returning values. Again, this is one of the things that kind of confuses students. A lot of times they'll, they'll call it, but they won't assign the return value to variable. And then that value is lost. You see that a lot. And then the idea of a signature and based on the signature, we can create multiple overloaded methods. So one same name, different signature, and depending on this values that are sent, that determines which one is called. And then finally, we saw the little example, introductory exercise. It's a nice fun one. It takes a few minutes. But the students will will remember that one because it's kind of active. That's the end of this one. Thanks for watching.