More About Methods

Methods are the OOP name for functions. A method is always declared inside a class; methods can't exist outside a class. A method has this general form:

image

The parts marked “opt” in the previous example are optional, and we will deal with these in due course. An example method may look like this:

void setValue(int i) {
     something = i;
}

The main routine, where execution starts, is another example:

image

Arguments and parameters

What's the difference between an argument and a parameter? Here's a bit of terminology which is frequently ignored. A parameter is any variable declared within the method signature. The list of parameters is enclosed in parentheses. Each parameter consists of a type name followed by a variable name. In the code fragments shown previously, i is an int parameter to setValue(), and args is a String array parameter to main(). Parameters are sometimes called “formal parameters”.

Arguments only appear in calls to a method. An argument is the actual value used in a particular call to a function. If we invoke setValue twice like this:

    setValue(17);
    setValue(x);

Then 17, and x are arguments passed in these calls, respectively. Arguments are sometimes called “actual parameters”.

The String array parameter in main is usually given the name arg or args, even though it's a parameter, not an argument. (This tradition comes from C, which has the same misnomer.) Args holds the command line arguments passed to the program when you started it up. If you run your Java program by typing

java myClass 12 file.txt Birmingham

then the String array that the run-time library passes to the main method of myClass will have three elements with the String values “12”, “file.txt” and “Birmingham” respectively.

Overloaded method names

You invoke a method by its name. You can have several methods with the same name in a class. The same-named methods are said to overload the name.

void someMethod(int i) {
        // some statements
}

void someMethod(double d, char c) { // overloaded
        // some statements
}

Those two methods in the previous example are overloaded. As long as they have different arguments, the compiler will be able to tell which one you intend to call. The ten-dollar way of saying this is, “The method signature is used to disambiguate overloaded method calls.”

Methods with the same name should do the same thing. Don't have a method called validate() that validates a customer record, and another one in the same class called validate(int i) that calculates the square root of its argument.

Unlike primitive variables inside an object (data fields), primitive variables declared inside a method have an undefined initial value. For those familiar with compiler internals, it's too big a performance hit to keep clearing stack frames, so it's not done. Primitive local variables get an initial value of whatever old junk was left on the stack. Your compiler may try to warn you about places where you have possibly left a local variable without an initial value. But you cannot rely on this, so it's important to initialize local variables before use.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.171.153