Contents Index Understanding fields Object oriented and procedural languages

ASA Programming Guide
  Introduction to Java in the Database
    A Java seminar

Understanding methods


There are two categories of Java methods:

To declare a method, you state its return type, its name and any parameters it takes. Like a class declaration, the method uses an opening and closing brace to identify the body of the method where the code goes.

public class Invoice {

    // Fields
    public String lineItem1Description;
    public double lineItem1Cost;

    public String lineItem2Description;
    public double lineItem2Cost;

    // A method
    public double totalSum() {
        double runningsum;

        runningsum = lineItem1Cost + lineItem2Cost;
        runningsum = runningsum * 1.15;

        return runningsum;
    }
}

Within the body of the totalSum method, a variable named runningsum is declared. First, this holds the sub total of the first and second line item cost. This sub total is then multiplied by 15 per cent (the rate of taxation) to determine the total sum.

The local variable (as it is known within the method body) is then returned to the calling function. When you invoke the totalSum method, it returns the sum of the two line item cost fields plus the cost of tax on those two items.

Example 

The parseInt method of the java.lang.Integer class, which is supplied with Adaptive Server Anywhere, is one example of a class method. When given a string argument, the parseInt method returns the integer version of the string.

For example given the string value "1", the parseInt method returns 1, the integer value, without requiring an instance of the java.lang.Integer class to first be created, as illustrated by this Java code fragment:

String num = "1";
int i = java.lang.Integer.parseInt( num );
Example 

The following version of the Invoice class now includes both an instance method and a class method. The class method named rateOfTaxation returns the rate of taxation used by the class to calculate the total sum of the invoice.

The advantage of making the rateOfTaxation method a class method (as opposed to an instance method or field) is that other classes and procedures can use the value returned by this method without having to create an instance of the class first. Only the name of the class and method is required to return the rate of taxation used by this class.

Making rateofTaxation a method, as opposed to a field, allows the application developer to change how the rate is calculated without adversely affecting any objects, applications, or procedures that use its return value. Future versions of Invoice could make the return value of the rateOfTaxation class method based on a more complicated calculation without affecting other methods that use its return value.

public class Invoice {
    // Fields
    public String lineItem1Description;
    public double lineItem1Cost;
    public String lineItem2Description;
    public double lineItem2Cost;
    // An instance method
    public double totalSum() {
        double runningsum;
        double taxfactor = 1 + Invoice.rateOfTaxation();

        runningsum = lineItem1Cost + lineItem2Cost;
        runningsum = runningsum * taxfactor;

        return runningsum;
    }
    // A class method
    public static double rateOfTaxation() {
        double rate;
        rate = .15;

        return rate;
    }
}

Contents Index Understanding fields Object oriented and procedural languages