Methods Part 1
1h 19m

This course looks at methods, which are named, self-contained blocks of code that you can call upon to help solve problems for you. We'll then take a look at some projects which will put methods into practice.

Learning Objectives

  • Understand the fundamentals of writing your own methods
  • Learn how to classify methods by their return types as well as their parameters
  • Learn about parameter passing schemes that are used by programming languages to determine how methods treat their parameters
  • Explore method overloading and two-dimensional arrays

Intended Audience

  • Beginner coders or anyone new to Java
  • Experienced Java programmers who want to maintain their Java knowledge
  • Developers looking to upskill for a project or career change
  • College students and anyone else studying Java


This is a beginner-level course and can be taken by anyone with an interest in learning about Java.


In this lecture, we will begin a more formal discussion of methods. At a minimum, when you hear the word method in the context of this course, you probably think of the main method, which is a great example. The main method is called or invoked by the Java runtime as the entry point to the programs that you write. You've also seen how we can call other methods, like print and println on the System.out object as well. An awesome thing about methods is that you can write your own, so-called user-defined methods. When we want to create a program to solve a problem, we have to understand the problem that we're trying to solve in order to write effective code. And a lot of times it's useful to break a large problem into smaller, more manageable pieces. As a result, it's also helpful if we could write a special block of code to handle these smaller problems. That's where methods come in. They allow us to write a named block of code that enables us to call it again or invoke it whenever necessary, in order to solve the problems at hand.

And another great thing is that methods can be called more than once. In fact, if you have a block of code you need to use in multiple parts of your program, you can just break it out into a method and call the method whenever you need to. So, it's reusable. So, enough talk. Let's do some coding. So, you can get a better idea of what we're talking about.

I'm going to write a variety of methods and we'll discuss why in more detail in just a bit. If you haven't done so already make sure to create a Section6-Projects project in IntelliJ. And then we'll create this MethodsDemo file. So, let's go over here to source which I've got here, 'Java Class' and then MethodsDemo. public static void main. There's that method that we've been using all throughout the course. That's our main method. And as a note, this is called the header of a method right here, this part and the part inside the curly braces is called the body of the method. So, that's the difference between the header of the method and the body of the method. And together they make up the full method. So, we're going to write a couple others here. So, we're going to do a void parameterless method. This is going to be public static void printHello. All right. So, there we go and printHello. And we're going to fill it in with System.out.println and it's just going to say "Hello there!".

So, these are particularly complex methods or even practical methods. They're just so we can get an idea of different concepts. So, void parameterized method. public static void printNumber. This one just happens to take a parameter. So, we call it parameterized. printNumber and we have System.out.println, "The number is" then a. And we have value-returning, parameterless methods. I don't think I put the word method on each of these. So, I'm going to just delete that. Keep it consistent. public static int giveMe10. Now this again is a trivial method, but it's returning the value here. So, this is giveMe10. So far so good. And next we have the value-returning, parameterized method. int num1, int num2. Here we go. And this one's going to say return num1 num2. Again very simple. Pretty much trivial. But it's returning the value that is the two parameters passed in, added to one another. Right. Okay.

Now back up to main. We're going to now fill main in with some usages here to test it out. printNumber(10), int result = giveMe10(). And then we have a System.out.println. I'm going to print out results there. I'm going to reuse result down here and do an add these called as addThese, our user-defined method and use result again. Good. All right. And of course let's run it, then we'll talk about it. So, right click here and we're going to run it. There it is. Okay. There we go.

You can easily see the output. Hello there!, and the Hello there! comes from the printHello method. main calls printHello, printHello calls this println statement right here and prints out Hello there!

The next one passes in the number 10 and it says the number is 10 because it prints out the... It prints out this right here. The number is 10. giveMe10 is fairly trivial. It would normally be a calculation or pulling from a database or over a network or something like that. Something a little more complex than this. But this is just returning the literal 10. It grabs the 10, prints out the 10, and then addThese takes 3 and 5, adds them together as we see down here. Right. And then it will store that result and print the result. Pretty awesome.

Another note is that when you have parameters in a method, we call this in the method definition. The whole thing is called the definition, again this is the header and then this is the body. Part of the header is the parameter list. Some methods don't have parameters and those are called parameterless. And those with parameters are called parameterized. So, if it has this parameter right here, what happens when you pass in the value? Well, let's take printNumber for instance. printNumber right here has the value 10 passed in. This value that's passed in is actually called the argument to the method. And then down here with addThese. These two are arguments to the method. So, argument 10 is passed in. Now, we could also pass variables here and they would still be called arguments. So, the arguments three and five passed in, when you call it, call the method it's called an argument but the variable in the method definition that takes on the value of the value that you pass in or variable that you pass in whichever that is called the parameter. So, that's the difference between arguments and parameters. Although some software engineers use the terms interchangeably, they technically are not the same thing.

So, we've seen four different combinations basically of these methods based on two different types of categorizations. If we classify a method by the return value, there are two choices. We have void which means the method does not return a value. So, this one right here and this one right here are both void. They just do something. They don't return a value or what they would do up here is when a value-returning method which is the other kind is called, you can think of it as replacing itself with the value that it returns. So, giveMe10 down here actually returns a 10. It's as if this is replaced by that 10 and then result gets the 10. So, that's value-returning. So, these are value-returning. Integers, these are both return int but it can return double, string, horses, cows, donkeys, scanners, doesn't really matter as long as it's some sort of type, primitive or reference or arrays or array lists, anything can be the return type and anything could be the parameter. So, those are the void versus value-returning.

The other way to classify them is by their parameters. So, that is the data that's passed in, placed in between the parentheses of the method's header. There are also two possibilities for this. The method could be parameterless, so no parameter in between the parentheses, or it could have at least one parameter. So int a or int num1 and int num2, anytime there's any parameter we just call it parameterized. Now you could be more specific and say it's got three parameters, we've got seven parameters, we have got 115 parameters. Probably don't want to write a method with 115 parameters but could be wrong.

So, if it doesn't have any parameters it's parameterless and if it does it's parameterized. So, we can essentially have four different combinations of these types of values. You have void parameterized, void parameterless, value-returning parameterized, and value-returning parameterless. A brief note about the code we wrote, the static keyword that you keep seeing here. Since main is static, we have to actually make these other methods static, if we're to call them directly from main it means that the methods belong to the class not a specific object. That may not make very much sense to you right now, but that's okay. But for our purposes in this section, we need to use the static keyword in order to call the methods from main that we've been doing. We will discuss the static keyword in more detail later in the course. For now, just go with it. I've got your back, don't worry.

Before moving on, I have a challenge for you. In this same MethodsDemo file, I want you to write a method that returns the square of a double that's passed in. The square of a value is of course the value multiplied by itself, then capture that value that the method returns and print it out to the console. So, pause the video, come back when you're done or if you need some help. How did that go for you? Were you able to complete this challenge? Let's work on it together. So, let's bring that down just a little bit there. We're going to create this new shiny method here. It's going to be public static and since it will be squaring a double, the result will be a double also, it'll be a real number. 

Because if I have 3.5 times itself, you're not going to end up with an integer, you're going to end up with another real number. So, we have to be able to accommodate for that. double num and it's very simple. I'm going to put this comment there rather at the end of the method and again, you don't have to put those comments at the end of the method is just for us to keep track and for you to see where the curly braces end for each method easily. Now, what we have here is just return of the number. Now I could do this, I could say double result = num * num. Oops. And then I could return result but you could also forgo the necessity of having the separate variable and you could just say this. You can just say num * num. Right?

Pretty simple. And of course we want to test it though. There we go now. We'll go up here into main and we're going to test it. So, we're going to say, we'll use result again and say square(5). We know that's going to be 25. So we're going to definitely make use of I guess I can't do that because that's a that's an integer. So, I need to make a double, we'll call it dResult and then I'm going to change it down here to dResult. There we go. Good. Okay. So, System.out.println(dResult). So, the integer couldn't hold it since the data could be lost. That's called a narrowing conversion or a lossy conversion. So, Java does not allow lossy conversions without you explicitly saying I know I'm going to lose data. So, that's why you can't store a float or a double in a float because you could lose data or a double in an integer or a float in an integer or anything like that. So, we just took care of that by making a separate variable. 

The result is the square of five and we know it's going to be an integer but it's making use of double. So, let's actually make it a little more interesting and make it 5.5 and then print it out. So, let's run this again from the src here, MethodsDemo, 'run'. And see what we get. 30.25. Sounds good to me. Right. So, that's the 5.5 goes down here is multiplied by itself and is returned. So, another thing you could do just to note if we didn't want to use this separate variable here, you can actually call it immediately. So I could say System.out.println and I can actually call square directly on this right here because all print line needs is just this value to print out, right? There's no difference between me putting a variable there that's a double and the return type that's a double because it'll be a double ultimately. If I run this guy, we'll see that we have the 52.5625. That looks pretty good to me. You can verify it on a calculator if you'd like. Awesome.

Methods are extremely powerful and they help us solve all sorts of problems and to break the solution to a large problem down into more manageable little solutions to make the full solution. In the next lecture, we will continue our discussion of methods and some of their awesome features, including parameter passing and method overloading. I'll see you there.


About the Author
Learning Paths

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics