Special Topics


More About Methods

In this section I’d like to focus your attention on two behavioral aspects of methods which you will find helpful to fully understand before attempting more complex programming projects: 1) how arguments are passed to methods and, 2) local variable scoping rules.

How Arguments Are Passed To Methods

Two sorts of things can be passed as arguments to a method: 1) a primitive type object or 2) a reference that points to an object or array of objects. (Otherwise simply referred to as a reference.) When the argument is passed to the method a copy of the object is made and assigned to its associated method parameter. This is referred to as pass by copy or pass by value.

Consider for a moment the following method declaration:

    public void someMethod(int int_param, Object object_ref_param){        // body statements omitted    }

The someMethod() method declares two parameters: one primitive type int parameter and one Object reference parameter. This means that someMethod() can take two arguments: the first must be an integer primitive and the second can be a reference to any Object. (Remember — all user-defined types are ultimately Objects! Also remember that a reference contains a value that represents the memory location of the object to which it points.) The values contained in these two arguments (an int and a reference) are copied to their corresponding parameters during the early stages of the call to someMethod(). While someMethod() executes it is only operating on its parameters, meaning it is only operating on copies of the original argument values.

For primitive types this simply means that any change of value made to a method’s parameter will only affect the copy — not the original value. The same holds true for reference parameters. A reference parameter will point to the same object the reference argument points to unless, during the method call, the reference parameter is changed to point to a different object. This change will only affect the parameter or copy — not the original reference used as an argument to the method call. (Bear in mind, however, that while a reference parameter points to the same object the argument points to, changes to the object made via the parameter will have the same effect as though they were made via the argument itself.) Figure 9-12 illustrates these concepts.

image from book
Figure 9-12: Primitive and Reference Argument Values are Copied to Method Parameters

Referring to figure 9-12 — Prior to a method call primitive and reference fields contain values. During method set-up these values are copied to their corresponding method parameters. The parameters can be manipulated by the method during the method’s lifetime. Changes to the parameter values will only affect the parameters, not the original arguments. After the method call primitives and references used as arguments will have their original values. Changes to the object pointed to by the reference parameter will remain in effect.

Local Variable Scoping

Methods can declare variables for use within the method body. These variables are known as local variables. The scope of a local variable includes the method body block or code block in which it is declared, however, it is only available for use after its point of declaration. Parameters are considered to be local variables and are available for use from the beginning to the end of the method body.

A local variable whose name is the same as a class or instance field will hide that field from the method body. To access the field you must preface its name with the this keyword. (Or change the field’s name or the local variable’s name to eliminate the problem!)

Anywhere An Object Of <type> Is Required, A Method That Returns <type> Can Be Used

The title to this section says it all. Anywhere an object of a certain type is required, a method that returns a result of that type can be used. Substitute the word type in the previous sentence for any primitive or reference type you require. For reference types a new clause can be used as well. Refer to the following method declaration once again:

    public void someMethod(int int_param, Object object_ref_param){        // body statements omitted    }

Assume for this example that the following fields and methods exist as well: int_field, object_reference_field, getInt() and getObject(). Assume for this example that getInt() returns a primitive int value and that getObject() returns a reference to an Object. Given these fields and methods the someMethod() could be called in the following ways:

 someMethod(int_field, object_reference_field); someMethod(getInt(), object_reference_field);      someMethod(int_field, getObject());      someMethod(getInt(), getObject());      someMethod(getInt(), new Object());

As you progress through this book and your knowledge of Java grows you will be exposed to all the above forms of a method call plus several more.

Quick Review

Arguments are passed to a method call by value. This is also referred to as pass by copy. The method parameters contain a copy of the argument values and any change to the parameter values only affect the copies, not the actual arguments. Any change to an object pointed to by a method parameter will remain in effect when the method call completes.

Methods can contain local variables whose scope is the body code block or the code block in which they are declared. Local variables are available for use after the point of their declaration up to the end of the code block. Method parameters are local variables that are available to the entire method body.

Anywhere an object of <type> is required a method that returns that <type> can be used in its place.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

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