03 – Methods

Methods are kind of like subroutines in Java. That is, in your typical Java code methods are really where all the work gets done. Let’s start by taking a look at a special method to Java, which is our “main” method.

public class Test1 {
  public static void main(String[] args) {
    // Our method code goes here

For the moment ignore the first line, which declares our class and take a look at the second line. That public static void main(String[] args) line declares a method (specifically a method of class Test1). The words before “main” are the attributes and return value of the method, and the information after “main” within parenthesis are the arguments to the method, in this case an array of Strings (the double bracket “[]” indicates an array).

To Java this method is special. When you execute a given class it will look for a method with this exact signature to execute. If it finds none Java will exit with an error. By “signature” we are referring to the modifying attributes before the method name, the name itself, and the exact arguments to the method. Here the modifying attributes are “public static void”, which means that the method is publicly available to be called by any other class in Java. It is static, so it exists as part of our Test1 class even without creating an “instance” of the class (more about what it means to have an instance of a class in a bit, never the less know that whether or not a method is static is an important attribute of the method). Lastly, it’s return type is void, so this method does not return anything. In some languages the “main” method or subroutine must return an integer – it must be void in Java, there’s another way that exit codes can be handled.

Simple enough, but what if we want to create our own method? What would it look like and how would we use it? For our example we are going to create a method that simply returns the value of two ints multiplied together. At the moment, we will make the new method static, but soon we’ll see that it’s not necessary or desirable to do so.

public class Test2 {
  public static int multiply(int a, int b) {
    return a * b;
  public static void main(String[] args) {
    int i = multiply(2, 3);

Compare the new multiply method with the existing main method. They are both public and static, but clearly the new method returns an int value and accepts two int values as arguments. You can see that when we go to use the method we simply call it with the appropriate arguments. Notice that this only works within the same class, so if we wanted to call the multiply method outside of this class we’d need to do something more complex, but we’ll get into that later as well.

Methods Exercise

Unfortunately we still don’t have quite enough information to write a Java program on our own, but we can practice writing a method call for Java. In this exercise write a method called average that averages two double values and returns a third double. The main routine that calls the average method and displays the output. Remember, for the moment, the method you make has to be static!

public class AverageTest {
  // put your 'average' method here!
  // ...
  public static void main(String[] args) {
    double a = 3.2;
    double b = 5.6;
    double c = average(a, b);
    System.out.println(“average(“+a+”,”+b+”) = “+c);

Like our previous session, this code should be able to be compiled and executed if you’re feeling adventurous (we’re not officially ready for that, but we will be soon). If you save this file as “AverageTest.java” You should be able to compile and run it with the following command line:

> javac AverageTest.java
> java AverageTest