16 Mar 1996

Object Methods

All program code is contained within the methods of some class. If a method is declared with the keyword static, then it acts just like the functions, subroutines, or procedures of other languages.

A method that is not declared static can only be used if it is associated by the caller with an object that is an instance of its class. The method name must be preceded by a reference variable (or an expression yielding a reference value) that designates the object. Not surprising, this is called an instance method.

class Date {

public byte month;
public static byte max_month=12;
public byte day;
public static byte max_day=31;
public short year;
boolean is_valid() {/*Thirty days has September*/ };

};

An application then declares "today" as a reference variable of class Date:

Date today;

if (today.is_valid())

The last statement calls the instance method is_valid through the object designed by "today", returning a value of true or false. Presumably, this method checks the values of the instance variables month, day, and year to verify that they represent a proper date.

Within a method, the field variables of the class have simple (unqualified) names as if they were local variables declared within the method itself. A static method can use any static field variables in the class, but it cannot use instance variable names because a static method has no associated object to provide the instance variables. A non-static (instance) method can use both static and instance variable names from its class.

When an instance method is called, as in the example "today.is_valid()", the object designated by the reference variable "today" is an implied argument to the call. Any reference to an instance variable name implicitly extracts the field from this implied object argument.

It is also possible for one instance method to call another instance method by name without supplying an explict object in the call. The current object is implicitly passed from one method to the next.

Continue Back PCLT

Copyright 1996 PC Lube and Tune -- Java H. Gilbert

This document generated by SpHyDir, another fine product of PC Lube and Tune.