Methods | Java | Tutorial 13

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to draft Academy my name is Mike in this tutorial I want to talk to you about creating methods in Java now a method is basically just a block of Java code that we can use to perform a specific task or a specific function and generally you're gonna take a bunch of Java code you're gonna put it inside of a Java method and then we'll be able to call that method and execute all the code that's inside of it now another word for method is function so if you ever you know use a different programming language sometimes methods are referred to functions but for our case you can call these either methods or functions and you know you're basically going to be on the right track so I'm going to show you guys how we can create a method in Java and then we'll use it to for example like say hi to a user so over here in my Java file you'll notice that I have a couple things here up here this says public class app and this is basically just the name of our you know like Java program and then down here we have this thing public static void main and this is actually believe it or not a method in Java and this is a very special method in Java so this is a method which gets called in other words the code inside of this method gets executed when we start our Java program so that's what makes this method special but in addition to this method we can also create other methods inside of this Java file so I'm gonna make a couple new lines and we're gonna come down here and you'll notice here I'm out of the bounds of this public static void main method so I want to create another method I'm gonna put a bunch of code inside of here and then you'll see we'll be able to call that method and access all the code inside of it from up here in our main method so the way that we can create a method is a little bit complex and I'm gonna be using some keywords here you'll see they get highlighted in purple that you might not fully understand and that's okay for right now as we go throughout this course we're gonna figure out what all of this stuff means but for now I'm gonna be using some certain keywords that you guys might not understand so just know that it's okay and you can just type out whatever I'm typing out and it should work so I'm gonna type down here public static void and now what I want to do is I want to type out the name of the method that I want to create so I'm gonna give this method a name let's make a method that just says hi to the user so I'm gonna make a method called say hi so I can type out the name of the method and then I want to type out and open and close parenthesis and then an open and closed curly bracket so now what we have is the basic skeleton for a method and like I said you know public static void this is sort of a little bit beyond what we're gonna learn about right now I just want to talk to you guys about creating methods but just know that if you put this here then everything should be fine now inside of this method I can type out some code and when I call the method that code will get executed so in here I'm just gonna put out put some code will say system dot out dot print line and we're just gonna print out and we'll basically just say hi to the user so we can say hello and why don't we say hi to me so say hello Mike so we're saying hi to Mike now what I can actually do is I can call this method so up here inside of my main method and remember this is the method that gets executed when our Java file runs I can just call this method so I can just go here and I can say say hi and then make an open and close parenthesis and a semicolon and now what's gonna happen is when Java sees this line of code it's gonna come down here to this say hi method and it's gonna execute all of the code inside of this method so why don't we come over here and we'll run this program and you'll see it just says hello Mike so it's executing all of the code inside of that function and I just want to illustrate what's happening here so I'm gonna add in some more print statements just so we can kind of really wrap our heads around how this works I'll add in a print statement up here and a print statement up here and so I'm just gonna say right here top and we'll say over here bottom and I want to show you guys how this ends up printing out to the screen so what's gonna happen job is gonna come it's gonna say okay system dot print line it's gonna print top then it's gonna go to this say hi function it's gonna print out hello Mike and then it's gonna come back out and it's gonna print out bottom and so this will kind of illustrate how the flow of the program works with these functions so I'm gonna click play and you'll see here we're printing out exactly that so top and then it comes over here execute the method and then it prints out bottom so really the flow of this program is we're executing this first line when we call the function we jump over to the function and we execute any code that's in here so if I put like another line of code in here and we could say like hello Tom this would now execute both of those lines of code before it executes bottom so when you call a function you're basically telling your program to jump over to that function execute all the code in there then come back and you move on to the next line and the program so that's kind of the flow of functions and that's really like the basics right I can just take all my code put it inside of its own function but I want to show you guys some more stuff we can do with these functions and one thing we can do is give this function some information so right now inside of this public static void say hi function and actually I just realized I'm saying function instead of method so again you can say function or method those two words are kind of like synonymous I've just been jumping around to different programming languages so I'm kind of more used to saying function but these can be called methods or functions so inside of this say hi method I'm just printing out hello Mike but let's say that I wanted to be able to give this method some information so I wanted to tell this method who I wanted to say hi to we can use something called a parameter and a parameter is basically just a piece of information that you give to the function so I have this say hi function right here I can actually give it some information and the way that we do that is by just coming over here into these parentheses and we want to define a value that method is going to take in okay so I'm gonna define a value and you basically define it just like you would define a variable so in our case we want to take in a string which is going to be the name that we want to print out so I can just say string name and basically what this is doing is it's telling Java that this method should take in one value it needs to be given one piece of information which is a string called name so now when I call this method over here I have to give it a string so I have to pass in a piece of information so I could just pass in for example like a name could pass in like Dave and what we can do is down here inside of our method we can actually access this name variable so I could come down here and say hello name so now depending on the value that we pass in here that's what's gonna get printed out so let's go ahead and run this program and you'll see what happens so over here it says hello Dave so let me make a couple more of these methods so I'm just gonna copy this and we'll just say hi to a bunch of different people so we can say hi to Dave Phyllis and Angela so now three separate times I'm gonna say hi to different people so I'll run this function and it says hello Dave hello Phyllis hello Angela so I was able to give the method specific information and depending on the information that I gave it it printed out a different name so if you wanted you could also pass variables into here so I can just say string my name is equal to Mike cuz that's my name and then I could just pass in my name into here and it's gonna work exactly the same in addition to just passing in one value we can also pass in multiple values so let's say that in addition to saying hi to the user I also want it to print out their name so I wanted to give this method two pieces of information that it can use to say hi to the user if I want that I can come down here in addition to specifying that we need a name I could also specify that we needed an int called age so now this method is taking in two parameters and so when we call the method we need to pass it two parameters so why don't we just do this once we'll pass it my name and then we'll also pass it an age so we could say like 47 so now I'm passing it two pieces of information and then down here we can just modify our print statement so I can say hello name you you are and now we can just say the age so age so now I'm able to use both of these pieces of information to modify this method so when we come over here now it's gonna say hello Mike you are 47 so I could modify you know either of these values I could say instead of 47 we could say like 20 and we can make the name Toby and you'll see that everything works the same so now it's saying hi to Toby who is 20 so that's basically how you can pass information into methods and methods are gonna be extremely useful like there's so many different things that we can use methods for and basically anytime you have a specific code that's performing a specific task you want to put it inside of a method and you know I could put as many lines of code in here as I wanted I mean obviously I'm just creating a simple method but you could make a huge you know long method and it'll perform a specific task but a general rule of thumb is anytime you have a collection of code that's doing something that's you know has a function or that's performing a task you want to put it inside of a method and that way you can just access it by the name of the method and you can give it specific information hey thanks for watching if you enjoyed the video please leave a like and subscribe to drop acad to be the first to know when we release new content also we're always looking to improve so if you have any constructive criticism or questions or anything leave a comment below finally if you're enjoying drop Academy and you want to help us grow head over to Draft Khadem EECOM forward slash contribute and invest in our future
Info
Channel: Mike Dane
Views: 23,879
Rating: 4.9798994 out of 5
Keywords: Programming
Id: Cx_ut1msINQ
Channel Id: undefined
Length: 11min 0sec (660 seconds)
Published: Sat Oct 21 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.