Methods

**Methods **are major components of object oriented programming. In each object, methods are what complete actions. Methods, in essence, are what drive a program. Each method has a defined set of actions which are contained in brackets { } as well as a signature line which is used to create and define the method.

Refer to the diagram of a method signature line below:

Method example

A method signature line will have five main components:

    1. Access modifiers:    public, private, or protected

    2. Access modifier:     static

    3. Return type:         void, String, int, double, ...

    4. Method name

    5. Parameters

The first component required for a method signature line is the access modifier (public, private, or protected). One of these is required to be placed at the beginning of each method. public, private, and protected mean different things; each has a different set of restrictions on which outside methods can access the particular method (you will learn what this means in the Method Types lesson).

The second component in method signature lines is the keyword static. This is optional in some methods. The static keyword will be also be covered in the Method Types lesson.

The third term in a method signature line is the return type of the method. The return type, is a variable type that will be returned by the method. return is a new term in computer programming. Here is the syntax of returning a value:

    return VALUE;

The method must return a value, and the value must be of the return type. Once the value has been returned, the method closes, and no other actions past the return statement will occur.

Here is an example of a method which returns an int value:

    public static int giveAnInt(int a) {

        if (a > 0) {

            return 3;

        } else {

            return 4;

        }

    }

    // returns the int 3, or alternatively the int 4

If a method is not meant to return a value, then the return type is ‘void’. Here is an example:

    public static void giveNothing () {

        System.out.println("return nothing");

    }

Returning values is a useful device because it allows shorter code while using the values generated by a method. Returning a value is to pass a value generated by a method to the location of the method call. Here is an example of return being used in calculations:

    int b = 3;
    int c = b + giveAnInt(3)
    // c = 6

The fourth component in a method signature line is the name. Methods and variables have naming conventions used by most programmers. Methods and variables both traditionally have the first letter in the name lowercase. The subsequent words in the name would start with an uppercase letter. Finally, method and variable names can, besides letters and numbers, only use these symbols: _ and $. Here are some name examples:

    public static void $$$          () {}
    public static void valid        () {}
    public static void method4      () {}
    public static void methodABC    () {}
    public static void a0f$_hy72    () {}

The final component, before the { } and inside the ( ), are the parameters. A method can have one, many, or zero parameters. A parameter is a variable of expressed type which is given to the method from an outside source so that it can be used within the method. The parameter must also be given a type followed by a name, written in the parentheses of the method which follow the method name. Here is an example of a method with multiple parameters.

    public static void method1(int a, String g, double d) {
        ...
    }

Furthermore, the parameters become what is known as a local variable. Local variables are able to be called and manipulated only within the contents of the method’s { }, and nowhere else. Most values given to the method as a parameter will not change despite manipulation within the method. However, later you will see that this is only true for primitive variables (ints, doubles) and immutable objects (Strings), which we have spent the majority of our time on so far. Objects such as arrays can be modified through methods; this will be covered more in detail in the Objects lesson.

In addition to this, whenever a parameter is given, it does not have to be used by the method. However, a value must be given for every parameter declared. If a parameter is not passed to the method, the call to the method will be invalid.

Lesson Quiz

1. What is the name of the below method?

    public static void _1(int a) { }
a. public
b. static
c. _1
d. a

2. What is the return type of the below method?

    public static String integer(int t) { }
a. String
b. static
c. Integer
d. int

Written by James Richardson

Notice any mistakes? Please email us at [email protected] so that we can fix any inaccuracies.