Calling a Method


Before you can call a member method, you must declare an instance of the class, unless the method being called is a static method. A static method is a method that is not associated with a class, which you ll learn about in Chapter 4. As you ll remember, creating an instance makes a real copy of the attributes and methods of the class. The instance is then used to call the method.

Let s see how this works in the preceding listing. The first statement in the main method declares an instance of the RegistrationForm class. The instance is called regForm .

Once the instance is declared, the name of the instance is used to call the dropCourse method, which is defined in the RegistrationForm class. The method is called by using the following items:

  • The name of the instance

  • The dot operator

  • The name of the method

The dot operator is a period that says, The method to my right is a member of the instance to my left and is used to reference attributes and methods of an instance of a class.

Passing Parameters

If a method has an argument list, then data must be included between the parentheses when the method is called. This is called passing a parameter. Data placed between the parentheses is called a parameter list. Each data element on the parameter list is called a parameter, and a comma must separate each parameter.

Most programmers treat parameters and arguments synonymously.

The dropCourse method requires two parameters because its method definition defines two arguments in its argument list. This means that we must pass two parameters when calling the dropCourse method. Each parameter must match the corresponding argument in the argument list.

Think of parameters in a parameter list and arguments in an argument list stacked on top of each other, as shown here. A parameter must be in the same order as its corresponding argument and must be of a compatible data type to the corresponding argument data type:

 dropCourse(102, 1234); 
void dropCourse(int courseNumber, int studentNumber) {

Values of the parameters are copied to arguments in the argument list. This is referred to as passing by value. Values in the parameter list appear in two locations in memory ”one location for the copy in the parameter list, and another location for the copy in the argument list.

The names of arguments are used in statements within the method to access values stored in arguments. In the previous listing (in the section Assigning a Value to an Instance Variable from Your Program ) names of arguments are used in a statement that prints the value of each argument on the screen.

Using a Return Value

The part of the program that calls a method accesses a return value by using an assignment operator placed to the left of the call to the method. As you ll probably recall from your programming course, an assignment operator copies the value on the right of the assignment operator to the variable or expression on the left of the assignment operator.

Let s see how this works. The next listing defines a class called RegistrationForm that contains the dropCourse() member method, which is used to drop a course, and returns a Boolean value as the return value.

An instance of the RegistrationForm class called regForm is declared in the second statement within the main method and is used to call the add method, passing it two parameters. Notice that two things happen in the statement that calls the dropCourse() method.

First, the dropCourse() method is called. Then, once the dropCourse() method has finishing doing its thing, the value returned by the dropCourse() method is assigned to the variable result using the assignment operator. The result variable is then used in the conditional expression in the if statement. A message reporting the results of the dropCourse() method is then displayed on the screen.

 class MyJavaApplication { 
public static void main (String args[]) {
RegistrationForm regForm = new RegistrationForm();
boolean result;
result = regForm.dropCourse(102,1234);
if (result)
System.out.println("Course dropped.");
else
System.out.println("Unable to drop the course.");
}
}
class RegistrationForm {
boolean dropCourse(int courseNumber, int studentNumber) {
return true;
}
}



OOP Demystified
OOP Demystified
ISBN: 0072253630
EAN: 2147483647
Year: 2006
Pages: 130

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net