Java Tutorial #16 — Introduction to methods

Topics Discussed: Methods, passing by value, method arguments, return types, void

Source Code Available Here

Methods are the primary way of breaking a program into individual component processes and is the key behind a concept known as Modularization. The general idea behind modular programming is breaking a large program down into smaller, more manageable parts; as this makes programming quicker, and debugging far more direct. On the other hand, excessive modularization can make for excessively verbose source code and difficult breaks in logic.

Using Methods
The syntax for using methods is pretty straightforward and will be described in depth below:

(access modifier)(static / non)(return type)(name)(argument list){
     //payload of method

Now let’s break the above example into smaller, more manageable parts:

  • Access modifier: The access modifier tells the compiler what can / can’t access the method, we’ll get more into this soon
  • Static / Nonstatic: This is fairly trivial, but it’s worth mentioning that you can’t directly call a non-static method from a static interface (calling a non-static from main for example).
  • Return Type: The return type is the type of value the compiler can expect to send back to the interface that you “called” the method from
  • Name: The name of the method can have the same name as any variable (doesn’t violate codespace, etc.)
  • Argument list: When the method is called, it can be passed variables(we’ll get more into this later) that can then be used locally in the method. The argument list tells the compiler what types of variables to expect and what to name them internally in the method

A few examples of methods and their respective calls would be as follows

//Example 1:

//Placed outside of main
public static void displayItems(){
     System.out.println("Items go here");

//placed inside main

//Example 2:

//Placed outside of main
public static void multiplyNumbers(int numberOne, int numberTwo){
     System.out.prtinln("The product of the two numbers is: " + (numberOne*numberTwo));

//Placed inside of main:
int a = input.nextInt();
int b = input.nextInt();


//Example 3 -- with return type

//Placed outside of main
public static int multiplyNumbers(int numberOne, int numberTwo){
     return (numberOne*numberTwo);

//Placed inside of main:
int a = input.nextInt();
int b = input.nextInt();

int product = multiplyNumbers(a,b);
     System.out.prtinln("The product of the two numbers is: " + product);

Return types
Return types are used to set a value of to be used in some process inside of the interface you called it from (for now that will most likely be main). This isn’t just limited to int, this can be any of the normal types that we’ve discussed so far, and even objects when we get to that point later on. So this could be boolean, string, a 1/2 dimensional array, etc. On the other hand Void means that absolutely nothing will be returned to the interface in which the method was called. This is useful when the program doesn’t need the values anymore, as they are destroyed when the method ends.

Passing variables, and the argument list
The argument list defines what we can / cannot pass to our method. There will be times when this is great for debugging, since you might have a variable that is waiting for 5 items, but only gets 4, the compiler will tell you that it’s awaiting the 5th. Also, very important to note is that up to this point we have been passing variables into our method by value, that means that the contents of our variable are being sent over, not the variables themselves. Therefore, any changes to our variables that might take place in the method only “count” in that method, here’s an example:

     public static void prodSum(int x, int y){
        //x = j,  k = y
        System.out.println("The product is: " +(x*y)); // produdct of our numbers
        System.out.println("The sum is: " +(x+y)); // produdct of our numbers
        x +=25;

           System.out.println("Enter number 1: ");
           j= input.nextInt(); // input 10
           System.out.println("Enter number 2: ");
           k=input.nextInt(); // input 20
           sum = prodSum(j,k);
           System.out.println("The sum is: "+ sum);

           System.out.println("j: " + j); // will still equal 10
           System.out.println("K: " + k); // will still equal 20

In the coming lessons we’ll be introducing how to pass by reference, which is where we will be passing the variable itself (sort of) to the function for extra processing.