6.6 Class Methods

Although we usually use instance methods in our dealings with objects, it is possible (and sometimes more appropriate) to use class methods - methods that have the same form as those that we encountered in Chapter 5, with the word static in their headings. As an example, suppose we want to create a class method for our Fraction class that adds two objects of type Fraction and places the sum in a Fraction.

Example 1

The class Fraction can be extended to contain an addition method as follows:

class Fraction
{

     private int num;
     private int den;
     public static Fraction sum (Fraction fl, Fraction f2)
     {
          Fraction total = new Fraction();
          total.num = fl.num*f2.den + f2.num*fl.den;
          total.den = fl.den*f2.den;
          return total;

     } }

Suppose now that we want to call this method from within the main method of a program. The way that we invoke the method depends on the way in which we organize our program. If we were to put the main method in the Fraction class, then we would call this method in the same way that we did in Chapter 5 using expressions of the form

<method identifier> (<parameter list>) 
For example, assuming that f, g, and h have all been declared to be of type Fraction in the main method, then the statement
f = sum(g,h); 
would assign to f the sum of the current values of g and h. Notice in the call the lack of an object and a dot preceding the method name. When we call a class method, there is no longer an implicit object parameter; only explicit parameters are possible. Usually we do not organize our programs so that all methods are in the same class. If the main method were in a class separate from the Fraction class, then a call to any class method in the Fraction class would require a different form. Suppose that our main method, in a class other than the Fraction class, contains three Fraction objects: f, g, and h. Now, to invoke sum to add g and h storing the result in f, we could write

f = Fraction.sum(g,h);

Here the expression that is used to invoke the sum method has the same form as those used to invoke the class methods in both the Math class and the In class:
<identifier>.<method identifier>(<parameter list>) 
The call first directs Java to the correct class and then to the appropriate method within that class.

Exercise 6.6

  1. Consider once again the class Circle that we have seen a number of times in this chapter. Each of the following headers could be used in methods that could be placed in the Circle class to determine and return the area of a circle.

    A: public static double area (Circle c)
    B: public double area ()

    1. Identify the class method and the instance method giving reasons for your choices.
    2. Assuming that a Circle object called disc has been created and initialized in a class outside the Circle class, write a statement that could be used to assign to the double variable discArea the value of the area of disc using method A.
    3. Repeat the previous part using method B.
    4. Complete the definitions of each of the methods.
    5. Write a definition of a class method called areaRatio that has two parameters, both of type Circle. The method should compute and return, as a double value, the ratio of the area of the first circle to the second.

  2. Write definitions for the following class methods that could be used in the Fraction class.

    1. The method product should have two Fraction parameters. It should return a value of type Fraction, the product of the parameters passed to it.
    2. The method abs should have a single Fraction parameter. It should return a value of type Fraction in which any negative fields in the parameter have been replaced by their absolute values.
    3. The method isPositive should have one Fraction parameter. It should return a boolean value: true if its parameter represents a positive fraction and false otherwise.