What is a method in Java? Well, we've already touched on what a method is in the Java hello world post, but I want to get more in depth on the power of methods (or functions). So first off, you've never heard me say the word “function” before, so before I talk about methods, allow me to start with an explanation of what a function is. In Java, methods and functions are really the same thing.  This topic is best described in this stackoverflow post.  So, for the sake of argument, I'll just use the term method from now on.  So, for example, a method in Java could look like this:

public Integer addTheseTwoNumbers1 (Integer firstNumber, Integer secondNumber)
{
  return firstNumber + secondNumber;
}

Now, this method is a little different than the ones we looked at back in the Hello World post.  There are two main things that are different. The first thing is the code public Integer addTheseTwoNumbers1, you see how we've put the word Integer next to the public keyword. This indicates that this block of code will return (or “spit out”) an Integer value. The second thing that indicates that this block of code is different, is the fact that it has a return statement. When you specify return you're saying that this block of code will be returning (or “sending back”) whatever is to the right of it. So how about we see what that same block of code would look like if it was a method that didn't return anything:

public void addTheseTwoNumbers2 (Integer firstNumber, Integer secondNumber)
{
  firstNumber + secondNumber;
}

Can you spot the differences? First, there's no code saying public Integer, now it says public void. The void modifier indicates that this block of code won't be returning anything. The second thing to notice, is that there's no more return keyword in the method.

So, what is a method in Java?  Well, a method is that a piece of code that could perform some operations, then return something back to whatever happened to “call” the method. Or, a method will perform some operations, without needing to return anything when it's done, the flow of code will just continue back from whatever called the method.  Either way, a method will execute some block of useful code that can be called repeatedly from anywhere in your program (so long as it's declared public).

To illustrate how the code flow could differ between method calls, let's take a look at this program:

public class myProgram
{
	public static void main(String[] args)
	{
          //---------------------------------------------//
	  // here's our method call that returns a value //
	  Integer resultOfAddition = addTheseTwoNumbers1(5, 24);
          //---------------------------------------------//
	  System.out.println(resultOfAddition);

          //----------------------------------------------------//
	  // here's our method call that doesn't return a value //
	  addTheseTwoNumbers2(3, 13);
          //----------------------------------------------------//
	}

	public static Integer addTheseTwoNumbers1 (Integer firstNumber, Integer secondNumber)
	{
	  return firstNumber + secondNumber;
	}

        public static void addTheseTwoNumbers2 (Integer firstNumber, Integer secondNumber)
        {
          Integer addedValue = firstNumber + secondNumber;
        }
}

If you were to copy/paste this code into your IDE (Spring STS), and run it, you'll see the following output:

29

This may or may not surprise you. If I were to look at this for the first time, I'd probably guess that you would see 29 and then 16. I would guess this because I see addTheseTwoNumbers1(5, 24) and addTheseTwoNumbers2(3, 13). So naturally, 5 + 24 = 29 and 3 + 13 = 16. So why didn't we see these two numbers as the output? Well that's because for one of the calls we used a return statement and for the other we didn't. Since one method returns a value, we can then use that value and display it in our console (by using System.out.println()). Whereas with the second method, we don't have a value being returned, so all that happens is that the two numbers are added together, and then we exit the method and the code continues to flow without invoking the System.out.println() code.

So why don't we just always use methods that return values? They seem more useful, because they give us something that we can work with. Well, that's just because a method that returned a value was more useful to use with this particular example. There are times when you don't need something returned, like for example, if you created a method to send an email to someone:

  public void sendEmail(String contactAddress)
  {
    String emailContents = "this is a test email.";
    Email.send(contactAddress, emailContents);
  }

You see? We don't want anything back, we just want to have the code send the email and go about its business. Okay, so I hope I've helped answer the question “What is a method in Java?” and that you understand everything entirely. But, if you don't, just leave a comment below and I'll answer your questions. Better yet, sign up for my mailing list, and I'll let you know when I post new content so you'll always be in the loop.

 

Free Java Roadmap

Discover exactly what you need to learn and where to start in order to become a professional coder.