Class methods

Continuing through the textbook, we'll cover Chapter 10.

 

Using class methods

Java includes three kinds of methods: Class methods are basically the Java equivalent of the functions, subroutines, or procedures in other languages.

The Math class includes several class methods, such as pow:
static double pow(double base, double exp)
Returns the result of raising base to the expth power.
The word static marks this method as a class method, as opposed to an instance method.

You use class methods without needing to create any instances of the class:

	io.println("The square root of 5 is " + Math.pow(5.0 , 0.5));

 

Defining class methods

Pre-defined libraries are very helpful, but it's also very useful to define your own class methods. You can think of a class method as a packaged piece of code for accomplishing some task. There are at least two reasons for creating a class method:

As an example, let's define a factorial method, and use it in a program to calculate the number of ways one can choose r from n things:

	import pgss.*;

	public class Choose {
	    public static double fact(int k) {
	        double ret = 1; // this will be the factorial of k
	        for(int i = 1; i <= k; i++) {
		    ret *= i; // multiply the value of i into ret
		}
		return ret;
	    }

	    public static void main(String[] args) {
	        IOWindow io = new IOWindow();
		io.print("Choose how many of how many? ");
		int r = io.readInt();
		int n = io.readInt();
		io.println((Choose.fact(n)/Choose.fact(r)/Choose.fact(n-r))
		    + " choices");
		return;
	    }
	}

 

Parameters and variables

The only variables available within a method body are those declared within the method (including the parameters). And these are not available outside the method. They are called local variables.

So the parameters of the method need to include any information it needs. For an example of a method with more than one parameter, suppose we add a class method to the Choose class to do the ``choose'' calculation:

	public static double choose(int n, int r) {
	    return fact(n) / fact(r) / fact(n - r);
	}

When such a method is called, the parameters are replaced by whatever is in those positions in the call:

	    io.println(Choose.choose(6, 2) + " choices");

In Java, methods use call by value: the values of the expressions in the call are copied into the method. So changing them inside the method does not affect the outside world. So in this example:

	import pgss.*;

	public class ZeroExample {
	    public static void setToZero(int n) {
	        n = 0;
	    }

	    public static void main(String[] args) {
	        IOWindow io = new IOWindow();
		int i = 1;
		setToZero(i);
		io.println(i);
	    }
	}
the variable i is still 1 at the end, and a "1" is printed.

One fine point: arrays are a little trickier; the value of an array variable is a reference to the array, so you can affect the values of the array elements. If we change the above program to use arrays instead of integer parameters, we get a different result:

	import pgss.*;

	public class ZeroExample {
	    public static void setToZero(int[] arr) {
	        arr[0] = 0;
	    }

	    public static void main(String[] args) {
	        IOWindow io = new IOWindow();
		int[] a = new int[1];
		a[0] = 1;
		setToZero(a);
		io.println(a[0]);
	    }
	}
Here, the local variable arr is still a copy of the value of the variable a in the main program, but they both name the same actual array. So a[0] in the main program is changed (although a is not changed), and a "0" is printed.