A Rookie's Guide to Java Part 4 - Methods | Harry Wainwright | Skillshare

Playback Speed

  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

A Rookie's Guide to Java Part 4 - Methods

teacher avatar Harry Wainwright, BSc Software Engineer

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

5 Lessons (29m)
    • 1. Class Introduction: Methods

    • 2. All About Methods

    • 3. Methods - Parameters

    • 4. Methods - Parameters II

    • 5. Methods - Return Types

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.





About This Class

The fourth class in A Rookie's Guide to Java. Students will learn all about methods/functions in Java, one of the cornerstones of OOP. 

A Rookie's Guide to Java, previously released on Udemy currently holds a 5-star rating and boasts hundreds of happy students. Now I'm bringing it to Skillshare! A Rookie's Guide to Java assumes no prior programming knowledge. We start at the very basics and work our way up to some fairly advanced topics, including Java Swing. This course comes with written material to recap what you've learned at predetermined points. This truly is one of the best ways to learn.

I often see courses claiming that you can "Learn To Make Video Games (No Programming Needed!)" But unfortunately if you want to make anything more advanced than flappy bird you'll need a programming language under your belt. Learning programming is fundamental to any career in the software industry, games or otherwise, and Java is a great starting point.

Java is the most widely used programming language currently so you cannot go wrong by getting to grips with it. It holds a heavy focus on the "Object Orientated" part of "Object Orientated Programming" and as such it is a subject that features heavily in this course. 

Meet Your Teacher

Teacher Profile Image

Harry Wainwright

BSc Software Engineer


I have years of programming experience and have spent time working on projects ranging from video games to dynamic websites, and responsive databases. With a particular interest in object orientated programming, I intend to share all here on Skillshare.

Professional, concise, informative tutorials on a variety of object orientated programming languages for all levels of experience.

See full profile

Class Ratings

Expectations Met?
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.


1. Class Introduction: Methods: I can't and so methods or functions if you've done it that program in before they may be called functions in job they called methods were really stand against the cannon. The meat of other of this course functions or methods are essential to objects on it. Eight. Program It goes back to this idea of reusing code from methods is certainly certainly a way of doing. Uh, uh, It can get bit confusing a time, but again, we spread it out over multiple lectures. I do employ you to take a look at the written materials if you're struggling, Um, but we do take it slowly so you shouldn't have any trouble picking Europa's. We get along. 2. All About Methods: Okay, so we'll introduce functions or methods in Java today, except cleaned up a lot. My old code. So it's a bit easier to read and the functions or methods. We've been using them already, but they've just been in the background when really interacted with them. This here, this public static void main is a function. It's ah, it's a main function or method in job, the cold methods. I'll refer to the Miss Functions. So if you hear function method somewhere else just now, they mean the same thing. But we've been working with functions already. This main function is it's one that every single program will have, and whenever the program runs, it runs the main function. But we can write our own functions really useful because they allow a tow Reza section of code. And we've been going over this idea of reusing a section of code all throughout the course we did it with while loops. We did it with four loops. We did it with even a raise. There were a way of cutting down on the amount of coach you have to write and just calling back Teoh Abbott occurred somewhere else on functions perform the same job. Let's write our own now. We're gonna right? Really Simple program is gonna check out answers. Um, so we do this by coming straight out of the main function. So far, we've been working with inside the confines of it. So whenever a program runs, it runs wives in here. But we can go outside of this end bracket here. See, you start one xy where ends the come outside of the main bracket we can right around. So for the moment, let's just copy essentially our main function there. So, you see, we use the public bit. The public are up there, there's more. They can use such private functions. That was a static bit. Which means it's not gonna change. No, it's not necessary. But we're gonna keep it anyway. We're gonna use the void bit, which means it doesn't have a return type. We'll cover that in a later lecture. But just understand that if it's avoid function, it doesn't have a return type. And then we have the name of the function itself, which I've just called correct in the brackets. Here, you'll usually put your parameters as its avoid function we weren't. It is not necessarily because it's avoid function. The we won't need any promise. Ah, anyway, and then revives the guy squiggly brackets here, which just open and close it. So it's gonna be a really simple function. Was gonna print out a word and I'm going to write, You know, the one helps If I can spell that. I just copy this line here so they would have it. We've got our functions ran down here, so let's from the program, See what happens. We still got the erased it from last time, so you'll notice. They just says, Well, exactly what it did last time. He just prints out me Ray TEM positions going up well, sticking at 50 eso. What happens to these white Webvan Lee printed out Correct or incorrect because we haven't cold them when you press this big play. But I know you run any program, it runs the main function and it runs webs inside the main function. All of this is being ignored. So to call our functions, that's what we're doing. We have to call the functions. We simply type in the name off the function, so I'll just do the correct one. There we go. At the end, we have, ah, correct line. So that's what functions are in the very most basic. Ah, terms will be doing a bunch more videos on these. Released a few show you different return times and parent parameters and what you can do with functions because their formal in depth Um, just what we've done here, what we've done is created a single use function that we can repeat over and over again. Oops. To repeat the same section of code. And you may be thinking, Yeah, but can we do this with a loop? Well, of course we could, but this is a function in its most basic form on. As we begin to get into parameters and return types, you're going to see they get a lot more involved. 3. Methods - Parameters : again. So I'm gonna introduce parameters today, at least in the most basic form. More parameters will do Will allow us to pass information from main function into our created functions down here for the moment, If we were to, let's say, create a string. Which alcohol name? Ah, just like that. Kept less. Yeah, create a string cold name on. I can print this out just fine. On if a printout string is just gonna print out the word Harry, we've covered this before way. Understand what that means? Let's say I want to have access to this string inside of my function down here. So let's take this very same system out print line here. We'll put it in this function here. See, we get an error is underlined. Red. Squiggly says kind of find symbol symbol, very ill name, location, class, blah, Bubba. This is because we've declared mastering up here in the main function, but we're trying to use it down here in the correct function. So why doesn't this worked? Well, simply, variables are unique to the function that they declared in. So if I wanted a string named Harry down here, I'd have to write it in the function as such. Now, this isn't particularly useful if we have to copy out every single variable and write it in the function that we need to use it in, then there'd be no point in having functions. We may as well just right toe all inside the main. So arguments and parameters are are a way of passing information from one function to another. So to do such to do so, to do such so too may ease of these. We have to, in our declaration of the function, tell the function that is gonna have an argument. So to do so we just right in the in the start. But here you see, now we have Arizona door. All of these, that's fine. We have Arizona all of these because we're not passing anything in. We've declared that were passing something in, but we're not actually passing anything in. So let's get around this by typing in our name. Variable. Okay, Dokey them. So now we've created string called name were passing in a string called Name on our function is expecting a string cold name. I'll just change this toe word to demonstrate that are variable name up here. What we pass. It doesn't need to equal what we declare it as word is unique to the function. So if if I want to print out our name up here, I have to print out what we've called it in. Our declaration appear. Okay, Decadence. So let's give this a go. Brilliant. So you see, we've printed out are correct here. So the first line of the function has been called up here, printed out. Correct. The next line we print out word or name and it prints out what we declared it to Harry. So there we have it. We that we've been able to pass in a argument from one function to another. This is where functions start to get useful because we could reuse this string over and over again in a in a 1,000,000 different functions and keep passing it in. We wouldn't have to declare every time in each function. Obviously, just printing out word isn't particularly useful that moment. But imagine where you have a set of data. Let's say the age of a couple of students and then we have different functions. Toe work on this day or so. Maybe this one would times it by five. Maybe this one would subtract 20 from it. Maybe this one fictional one here would figure out what you were that they were born in. And that's what we're gonna be covering in the next lecture. We're gonna come with a couple of different uses of how we can manipulate data through functions. 4. Methods - Parameters II: Okay, then. So I've added a bit to our previous lecture. I say I've added a bit of a fair bit toe. Our previous lecture on parameters totally show you something a bit more interesting. So I've written the code out. It works. I'm gonna walk you through basically what did line by line. So here on the first line, I declare the max size of the array. So we found out in the ray lecture that going out of bounds of an eraser, going above or below the bounds of boundaries of an array is really bad. It will cause an error. So good practice again to is to declare the size of the array and a separate variable. And if you do this, Anil caps and it tells, or the programmers that that is a constant and it shouldn't be changed. That's what typing in all caps means. So I called my max erase eyes, big capital as five. And then when I declare the array down here instead of typing in five is we would have done in the previous lecture, Captain Maxillary excise, which is equal to five. Um so here I just declared I declare each of the, um, different positions or elements in the array. So zero, the 17 at one. This 22. So on so forth. So every position in the race full this five here I call the function. Let's just skip that for now. So outside of the main function, I'm actually declaring my new function, which is print array before we had incorrect and correct. This is print Array. First thing you notice if you're egalite is this Takes in two parameters. Previously, we were just taking in a single string, but now we're taking in t promise. And if he wants taken more than one, we have to separate them with a comma. Pretty simple. There were just separating each parameter with a comma. So we declare our function. Call it printed already. Ah, first parameter is an array. Uh, so this is a bit different. We're passing in an array. We're not passing in an interview, not passing in the strength. We're passing in an array. And we can tell this well, not only because I called it array that's good name and function for you, that, but because after our interview it is the trademark squared brackets. And that's how you can tell it's an array. You know you can't read the word array. A second parameter is the array size. This is important because I'm gonna be looping through the array to print out each element on is much more useful to pass in there a size of five. That is to, you know, manually type five s. So I don't have to keep scrolling back up to the top to see this particular line to see it's five spaces being. I can just go to the very top where you declare all the constants. I could see that maxillary sizes five of while I was passing max array size So pretty simple, right? Because I'm passing in multiple parameters. They have to be in the same order. When you call the function se see the first parameter and passing in is an array. So the first parameter van passing in when I call the function is also our array age of students. The 2nd 1 is there any size. So I make sure the 2nd 1 here is the maxillary excised. I know it can be a bit complicated to get your head around but it does all make sense. I promise. They're here in my full loop. I said it just like any other full loop. We thought with an entire equals zero, instead of typing five here again, we passed on our race eyes so I could just do eyes less than array size, which is equal to five. And then just like plus plus here we just system out every element in the array. So printing the array with I So first, let it be zero, then 1234 upto a race eyes so simple, really simple. We run it, It just prints are each elements of the right? So this is all code we've seen before? I mean, I bother function, but ah, but it should be pretty easy to get ahead around. So let's create another function. It's gonna work out the birth year. I believe we've done this before. In earlier lectures. Book again. We'll just put it all into a separate function. So let's call it calle que late type of spell it right. Always useful to name your functions appropriately. I haven't really thought about what parameters were gonna be passing in where I was gonna have to pass in the array. Andi, that's also pass in the current year. Uh, explain why right now. So if you ever want to talk, there is problem in the future. Well, you know, if this was a commercial program, you just find the eight years and, you know, some day and time methods just pull the correct date and time that the instant the program was run. But let's just say in the future you want to update this program so it's no longer same 2016. You can simply put a constant variable at the top, which was cool current year as well. There we have it. It's not if ever wanted to come back. And I ran this program and it said, you know, they were born in the wrong year ago. Actually, I should probably go change the current year to 2022. So now I've got to work out the birth year of each students. I'm gonna use the same full loop because it seems to make sense to me. Uh, yeah, that's person the right size as well. I know it's now we've got three different parameters that were passing into the function here. That's just copy the old loop. Save a bit A type in time. So the rest of for Lupus farm was gonna folks on the code in the middle. Uh, a we're going to do is system out print current year minus array I So this is gonna be 2016 . Take away. Let's say 17 for the first loop Brown. So be 1999. That's just tidy this up a bit. So look a bit prettier when we print it. Uh, student? What was born in on That should be fine. It's probably, ah is probably a quotation lock in the wrong place somewhere that doesn't cause narratives it. Let's do it on a separate line. Student space Captain ation. All right, fascination. Waas born in. And that should be okay, I run this first. Nothing's gonna happen because I haven't called theory, obviously. So let's just comment our peremptory so that will no longer room. Instead, let's replace it with calculates birth year. We've got to fill it with variable. So first ones obviously going to be page of students when I actually swap these around eso there, they're in the same older in the 2nd 1 We other race eyes is the second variable, so I'm gonna keep it. Is the same in this one, So we didn't calls any confusion. The second variable, their full being Max Ray size and 3rd 1 will be current e pull caps. Typo. It's one that still happens. May we gave them. So it takes in Massa's work. Fine, by the way. So let's just work through the loop Assumes there was one in 1999 so student zero was 17 years old. 2016 takes 17 is 999 a 2nd 1994 Yep. Now this may be a bit off because obviously I didn't include the months, but you can see it's working correctly. So finally I just very quickly run through what we did in summary passed in or a past in the race size passed in the current year. And then we just did a bit of work on it, culminating in printing out what year they were born in by cornea. Take way array I. So this was a bit more of an in depth example from what you could do with function parameters, how it can pass information around. We can initialize our one array up here, and then we can use it in two different functions, doing two different things. I'm sure you can think of. You know, I another thing to do if you're so inclined, that would allow you to reuse this array over and over again. But that's what functions of four that there to reuse code. So we don't have to type out this array every single time. And the information is going to stay the same constantly. In the next video, we're gonna be covering return types, which is Well, I wonder, Can you guess to me if we pass in information to functions with parameters? What do you think return types do? Well, they return information from the function to the main or whoever we wanted. It really. That's where we will be covering in the next lecture 5. Methods - Return Types: Hey, let's do return types. Eso covered parameters. Obviously the act off passing information into a function return times do the opposite. They ensure that you are able to pass information out of the function back into the main or if you wanted to get so let's create a new method will function down here on this, just gonna add 10. Um, so the first difference on here is we're not gonna create a void function. We're gonna create in function because we're gonna be adding 10 and we will be returning the previous lecture. I mentioned that void functions don't have return types, which is true. There are very few slight exceptions where you will see the return key word and avoid function. But it's not returning anything void functions don't return anything. They do a simple job such as print out Saray up here after we've worked on it. Any time you don't see avoid function, it's got have a return type, and that's what we're gonna be doing here. We're gonna be creating a function with a return to the coal mine at 10. I'm gonna take in and number. So all this function is gonna do it's gonna take in a an interview. I'm gonna add Tend to it and then we're gonna go back. So you see, we've got this error here. That's all the saying is where? Missing a return statement. So what we do now is write a code for our function, so it's gonna be number equals number close. 10. This is about the simplest version of a return function. I could think off. It's coming. Tower previous. Calculate Birth year printing Ramadan. Yes, Print. Let's not talk like that, sister out. We just got a passing the numbers. Let's I 991 night. I mean, we have, Ah, it prints out 109. So let's walk through what's happened here. First, let's try and call the function again that with our system outlined the It's a different number. Let's do 14. So you see, it's still any prints. 109. Well, why is that? Because we're returning the number here, so this line of code equals 24. But obviously we're not printing out. I mean, we could put it into a variable if you're so inclined. So that's int ah, pull returned number. So now we can run the same line of code here, and we can put whatever this equals, which will equal 24 into the variable return number. Now, if I wanted Teoh, I could just print now. Sorry about the phone call dinner. If you caught the tail end of that, anyone will was. So we've put our function into return number. This variable here. Return number. Now we can just print out. I returned number groups. Same as we've doing a 1,000,000 times before at this point. Capital P probably. Yeah. People guess there s no have printed out 24. So it's two separate ways of doing it here. We could put it into a variable and then print that out. Or we could just simply print out the function with whatever parameters we want. They both work the same way, obviously, but if we put it into a variable, we'd be able to reuse it over and over again. So it's all about passing information between one to the over. So let's work work through the function ourselves assed. What actually did so at 10 we passing the variable 99 up here, so number becomes 99 then we work on number, so it's 99 equals 99. Add 10 which is obviously 100 and nine, and we return number or 109 back up to here to our system out print line on it prints the return. So at the start of this line, we go into the function simply one more time. We go into the function passing 99 work on 99 return 909 and then back up here. This now equals 109 and we can print out obviously the same here. It's just we pass it into a variable when always they were passing 14. So that is the most basic version off return taken. They can basically do. We will pass in on were able to pass out variables from functions that aren't necessarily associated with each other. The important thing to know is that you can't do this with void formations. You can't have returned talks from avoid function, so if you want to return in, it needs to be an int function. If you wanted a string function, I was laid out. Need to be strength. You need to return a string uh, doubles floats, bulls exception A work on the same function. Well, the same principle probably shouldn't say function if you don't have ago. It's a more complicated return types. You're welcome to try and pass out the array from this Ah, calculate birthday. I'm not going to cover this in a lecture if you want to pay a bit more of a challenge than he could. In short, this isn't avoid function. You could make an int a ray function so it would have a return type of an array, and then you can try and print that out. But I'm warning you now that would be a bit difficult, because if you try to print that now, you're gonna get the address location of the array rather than the contents of theory. So if you can go on to Google and figure out how to get the contents of annoyed from return type, then I would give you a round of applause because that's some good Internet Googling you've got going on. But for now, I want to keep it simple. Influx balls, doubles, etcetera. These are return types. They're super useful when we'll be using them a lot in the future