Java Methods

A method is a block of code that you can use and reuse over and over again. The code contained within the method will only execute if the method is called. You can call a method as often as you like. Let’s make one now. Create a new file called MyMethod.java and write into it this code:

public class MyMethod {

  static void aCoolMethod() {
    System.out.println("I am a method.");
    System.out.println("Have a nice day!");
  }

  public static void main(String[] args) {
    aCoolMethod();
  }
}

In this example we have created a method named “aCoolMethod.” It is static, and that means we can use the method without having to first declare an instance of the “MyMethod” class. We will discuss this more later.

This method is a type void method. That means this method does not return any value to our code when it is called.

The empty round brackets after the method name means that this method does not have a parameter list. I will also explain more about that later.

The call to the method happens in the main entry part of our program, where all we did was type the name of our method followed by the empty round brackets.

In this next example, you can see we call the method three times in a row.

public class MyMethod {

  static void aCoolMethod() {
    System.out.println("I am a method.");
    System.out.println("Have a nice day!");
  }

  public static void main(String[] args) {
    aCoolMethod();
    aCoolMethod();
    aCoolMethod();
  }
}

When the code is compiled and the program run, the method executes three times and the output is:

I am a method.
Have a nice day!
I am a method.
Have a nice day!
I am a method.
Have a nice day!

Now let’s make our method return a value.

public class MyMethod {

  static int aCoolMethod() {
    System.out.println("I am a method.");
    System.out.println("Have a nice day!");
    return 5;
  }

  public static void main(String[] args) {

    aCoolMethod();
  
  }
}

First we changed “aCoolMethod” from type void to type int. Now “aCoolMethod” must return a value of type int, becaue a method’s return value must always match its type. An int method must return a value of type int and a boolean method must return a value of type boolean.

At the end of “aCoolMethod” we added the line, “return 5;”. This is the value our method is returning. And at the point in our code where we called the method, a value of “5” is being dropped in. But we did nothing with this value. It is a wasted value.

Let’s add an int variable to our code to catch that “5” shall we.

public class MyMethod {

  static int aCoolMethod() {
    System.out.println("I am a method.");
    System.out.println("Have a nice day!");
    return 5;
  }

  public static void main(String[] args) {

    int myInt = aCoolMethod();
    System.out.println(myInt);
  }
}

Now the output is:

I am a method.
Have a nice day!
5

Practice:

Create a new method. Have it return some value. Add a variable to store the returned value.

Next Topic: Parameters and Arguments

Comments

Popular Posts