Static methods

Figure 189. A method adding two values Slide presentation
public class Add {

    public static void main(String[] args) {
        int i = 2, j = 5;
        int result = sum (i,j);
        System.out.println("The sum of " + i +         
              " and " + j + " is " + result);
    }
    static int sum ( int a, int b) {
        return a + b;
    }
}
The sum of 2 and 5 is 7
sum : × ( a , b ) a + b

Figure 190. Syntax of method definition Slide presentation
   Method returns          Method expects two
   int values             arguments of type int
          ↘               ↙      ↙
    static int sum ( int a, int b) {
        int result = a + b;
        return result; 
    }                ↖
                      Corresponding to 
                      return type int

Figure 191. The static modifier matters! Slide presentation
public static void main(String[] args) {
     ...
    // Compile error: Non-static method 'sum(int, int)' 
    // cannot be called from a static context
    int result = sum (i,j);
         ...
}

// No static modifier
int sum ( int a, int b) {
   return a + b;
}

Figure 192. Documentation: Javadoc is your friend! Slide presentation
/**
 * Calculates the sum of two integers.
 *
 * @param a the first integer to add
 * @param b the second integer to add
 * @return the sum of the two integers
 */
static int sum (int a, int b) {
    int result = a + b;
    return result;
}

Figure 193. Separating usage and definition Slide presentation
Method definition Method usage
public class Helper {

    static int sum(int a, int b) {              
        int result = a + b;
        return result;
    }
}



public class Add {

    public static void main(String[] args) {
        int i = 2, j = 5;

        int result = Helper.sum(i, j);
        System.out.println("The sum of " + i +              
           " and " + j + " is " + result);
    }
}

exercise No. 87

Defining methods

Q:

Follow Figure 193, “Separating usage and definition ” and define the following two methods in a separate class Helpers:

  • A method square(...) expecting a double and returning its square value.

  • A method repeat(...) expecting a String and an integer number e.g. "Test" and 3. The string shall be repeated the given number of times generating "TestTestTest" for the given example values. If 0 == count holds an empty string shall be returned.

Provide Javadoc based documentation as being shown in Figure 192, “Documentation: Javadoc™ is your friend! ”. Then generate HTML documentation from it and view it in a Web browser of your choice.

On completion the following sample should work (if the Helpers class resides within the same package).

Code
public class Driver {
    public static void main(String[] args) {

        System.out.println("Square: " + Helpers.square(2.5));
        System.out.println("Result string: \"" + Helpers.repeat("Test", 0) + '"');
        System.out.println("Result string: \"" + Helpers.repeat("Test", 3) + '"');
    }
}
Result
Square: 6.25
Result string: ""
Result string: "TestTestTest"

A:

/**
 * Common helper methods.
 */
public class Helpers {

    /**
     * <p>Calculates the square of a given value.</p>
     *
     * @param value
     * @return The square of the given value.
     */
    static double square(double value) {
        return value * value;
    }

    /**
     * <p>Repeat a string a given number of times.</p>
     *
     * <p>Example: Given a string "Test" and the number 3, the result will be "TestTestTest".</p>
     *
     * @param s the string to be repeated
     * @param count the string will be repeated this number of times.
     * @return The result of concatenating the input string <code>count</code> times.
     */
    static String repeat( String s, int count) {
        String result = "";
        for (int i = 0; i < count; i++) {
            result += s;
        }
        return result;
    }
}