Lesson 5 – Methods

Alright, it’s time now to learn about Methods. Methods are a collection of statements grouped to perform a specific operation. For example; return a file from your machine, add two numbers together or pass some value back to the code you are writing.

Downloads

To get started you can use the basic starter project and follow the steps to compile the code examples for the lesson.

Setup

  1. Create a new package called: com.codenerve.beginner.lesson5
  2. Create a new class file called: Methods.java

If you need help creating these please go back and look at lesson 2

A Simple Method

Ok, let’s start off with something simple, our first method. In the new Methods class create the following method:

If you haven’t already, create a main method inside your class also:

Now if you run your code at this point (try it!) you’re going to find the output a bit disappointing!! So far our example program does nothing 🙁

Why? We’ll because your main method is not yet using the aSimpleMethod() method you created. To fix this you need to call your new method:

Run it again and you should see the contents of your new method executed in the console view of eclipse.

private static void
We’ve discussed these keywords briefly in previous lessons but let’s recap on their meaning:

  • private – means the method is only available from this class (Methods.java) and no other. It cannot be ‘called’ from another class. We’ll discuss other options later in the lesson series.
  • static – lets leave this for now. For now, all of your methods will be static
  • void – this is the return type of the method. Void means that the method does not return any value. We’ll show a method later in this lesson that passes values back to your main method.

Method with Parameters

Ok, lets create another method. This time we are going to pass some parameters to your method. Add the following code to your class file:

Again like the previous example we must call this from our main method:

Run the program again. This time you will see the second method being called by you main method and printing the sum of the two numbers you passed in. It’s important to note that you can also reuse these methods as many times as you like:

Methods that return values

Alright, let’s try something else now. What if we don’t just want to print a result but instead we want to get that value back and do something else with it. To do this we can return the result of a method. Add the following class to our code:

You’ll notice something different about this method. No I’m not talking about the extra parameter int k. That was added just to show you that methods can take as many parameters as you require. Im referring to the int return type. Previously we used void meaning return nothing. This time we want to return an int. To do this you will also notice the return keyword used inside the method.

Alternatively we could write:

This is just a more verbose way of writing the same thing as the previous example.

Ok, again like before, we need to call this from our main method but this time we need to capture the result. And lastly we will print the result out so you can see the result in the console view:

Try writing your own Methods

As with all my lessons, try writing your own methods. Try using different primitive types or String’s instead of ints. Also see if you can chain some methods together. For example:

What would be the result of the above code?? Try adding it to your main method and running it.

Convention and Good Practice

By convention you should always start methods with a lower case letter and start subsequent word with an uppercase value. You will see I have done this in these examples. Each method name must also have a unique name (not quite true we’ll discover a concept called polymorphism in the intermediate lessons).

Also for good practice and to allow other people to understand your code better try to use descriptive names. It should be easy to get a good idea of what a method does simply by reading the method name. In larger programs this is especially important as it will save a lot of time versus having to go read and understand every method.

Next: Loops

Menu