Methods

Document Sample
Methods Powered By Docstoc
					CS 142 Object-Oriented Programming I

Methods
Kinds of Methods
The Java programming language provides two basic kinds of methods: instance methods and class (or static) methods. The difference between these two kinds of methods are:   Instance methods require an instance before they can be invoked, whereas class methods do not. Instance methods use dynamic (late) binding, whereas class methods use static (early) binding.

When the Java virtual machine invokes a class method, it selects the method to invoke based on the type of the object reference, which is always known at compile-time. On the other hand, when the virtual machine invokes an instance method, it selects the method to invoke based on the actual class of the object, which may only be known at run time.

Return Types



void

A void method (or function) has no return statement (nothing is returned) and is basically a block of code that is enclosed in { } brackets called by it's name:

void functionName() { or void functionName(parameterList) { do something do something } }  can include a return statement to exit the method before reaching the end (return;)  typed A typed method (or function) has a return statement(s) and returns a value of a primitive data type (int, double, boolean) or reference data type (String, objectType)

returnType functionName() { or returnType functionName(parameterList) { do something do something return result; return result; } } //result must match returnType  any method that is not declared void must contain a return statement

Examples
void setName(String n) { name = n; } int addNumbers(int x, int y) { or int addNumbers(int x, int y) { int z=x+y; return x+y; return z; } } //return result of calculation directly boolean compareNumbers(int x, int y) { if (x<y) return true; else return false; } /*The else could be omitted since the return statement exits the function*/ String testNumber(int x) { if (x<100) return "The number is less than 100"; else return "The number is greater or equal 100"; }

Parameter Passing
Page 1 of 2

CS 142 Object-Oriented Programming I

A method (or function) cannot alter the value of primitive variable types (i.e., int, float, double, etc.) directly, instead it makes a copy of the passed variable such that the original value is not altered by the method. Only objects are passed by reference (and not by value). Strings and arrays are also objects in Java. public static void increment(int[] array, int amount) { array[0] = array[0] + amount; } //ends method int[] myInt = {1}; //initialized to 1 increment (myInt,5); //pass by reference array object System.out.println ("Array contents: " + myInt[0]); //prints out 6

Summary
Method Declaration
[accessSpecifier] [static] [abstract] [final] returnType methodName ([paramlist]) [throws exceptionsList] Java supports method name overloading so multiple methods can share the same name (the method’s signature differentiates which method is called). Also a class may override a method in its superclass. The overriding method must have the same name, return type, and parameter list as the method it overrides.

Method Body
Besides regular Java language elements, the keyword this in the method body refers to members of the current object. The current object is the object whose method is being called. Also the keyword super is used to refer to members of the superclass that the current object has hidden or overridden. Lastly, a method body may contain declarations for variables that are local to that method. These local variables exist only while control remains within the method.

Passing Information into a Method
A method argument can have the same name as one of the class's member variables. If this is the case, then the argument is said to hide the member variable. Although cannot pass methods into Java methods, can pass an object into a method and then invoke the object's methods. In Java methods, arguments are passed by value. When invoked, the method receives the value of the variable passed in. When the argument is of primitive type, pass-by-value means that the method cannot change its value. When the argument is of reference type, pass-by-value means that the method cannot change the object reference, but can invoke the object's methods and modify the accessible variables within the object. In other words, the references passed to a method are actually copies of the original references—two references pointing to the same object. Thus method manipulation will alter the objects but swaps will fail.

Page 2 of 2


				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:5
posted:1/19/2010
language:English
pages:2
Lingjuan Ma Lingjuan Ma
About