Method Basics
Programmers write methods, also known as functions, to automate tasks that may need to be performed many times. To take advantage of this pre-written code, there are several things that we need to know:
- The name of the method
- What information, if any, does the method need sent into it(parameters)?
- What information, if any, does the method return?
These three items together make up what is known as a method's signature. Notice, that as users of the method, we need not understand how it accomplishes its task. This concept is called
information hiding. Thus, if a programmer finds a more efficient way to perform a task, they can re-write the method and not effect any calls to the method as long as they do not alter the method's signature.
Another important consideration when calling a method is to know where the method is. Here are some options:
- Methods in the same class can be called using just the method name name (Ex: myMethod()).
- Member methods of other classes must be called from an instance of that class (Ex: str.length()).
- Static methods of other classes(library functions), must be called from the class name ( Ex: Math.sqrt(5)).
When passing information between a program and a method:
- When calling a method that takes parameters, the parameters must be in the same order as the methods signature.
- When a method returns information, that data needs to either be output or stored in a variable.
Java Methods
The signature of a method has several parts:
- access type - which users are allowed to call the method. The access type is public, private or protected. Public methods are accessable by all users. Private methods are accessable only by users in the class. Protected methods are accessable by users in the class and any subclasses.
- operator type - does the method operate on an object or not. The operator type is static if the method does not operate on the variables of an object. Otherwise, it is just left blank.
- return type - the type of information that the method returns. The return type is the type of data that the method will return to the caller. Return types can be int, String, double, etc. Methods that don't return information are labeled void.
- method name - the name of the method. The method name is an identifier of the method. It follows all the rules for naming variables and should describe what the method does.
- parameter list - the types and number of variables that the method expects to receive. The parameter list is the list, including types, of the variables being sent into the method.
Considering all of this information, a typical method header may look like this:
public static void showAge(int age)
This method is accessable by all users, does not use the private variables of a class, does not return any information, is called showAge and expects an int to be sent in.
Parameters
Variables declared in a method are local to that method. In other words, they have no value oustide of the method. So, if we declare some variables in a method, and want to use their values in another method, we send them into the other method as parameters.
Say we have taken user input on the coordinates of two points in our main method as int x1, y1, x2, y2. The input assigns them a value. If we want to write a method that will find the midpoint of these two points, we need to send the values of the point in. So, in our main we see:
int x1, y1, x2, y1;
x1=//code for input
y1=//code for input
x2=//code for input
y2=//code for input
showMidpoint(x1, y1, x2, y2);
}//end main method
In the call to showMidpoint, x1, y1, x2, and y2 are what we call the
actual parameters. In other words, these are the original values that I wanted to work with.
When we declare the showMidpoint method, we need to prepare it to receive information. We do this by declaring local method variables, called
formal parameters, in the parenthesis of the method signature. The signature will look like this:
public static void showMidpoint(int a1, int b1, int a2, int b2)
- a1 will take on a copy of the value of x1.
- b1 will take on a copy of the value of y1, and so on.
Because they are taking on copies of the values, altering the formal parameters will have no effect on the actual parameters.
So, our finished method looks like this:
public static void showMidpoint(int a1, int b1, int a2, int b2)
{
int midX = (a1+a2)/2;
int midY = (b1+b2)/2;
System.out.println("The midpoint of the points is: ("+midX+", "+midY")";
}
Finally, what if we wanted to do these calculations with double values, instead of int values? We can
overload the method. In other words, use the exact same signature with the exception of the type and/or number of parameters.
public static void showMidpoint(double a1, double b1, double a2, double b2)
The computer will differentiate as to which version of showMidpoint to call based on the data types that are sent in.
Returns
Since the variables in a method are local to the method, altering information will not have any affect on data in the calling method. So, to be able to send information into a method and view the result of any computations/calculations in the caller, we need to return the data to the main.
Data is returned from a method using the keyword return. A method can only return one value per call. The type of information that the method is returning is specified in the signature. If a method is not going to return any data, the return type void is used.
Returning information is independent of parameters. In other words, method can return information regardless of whether or not it takes information in.
public static int getAge()
{
int myAge=16;
return myAge;
}
Finally, when a method that returns information is called, it is important that the information that is returned is stored in a variable of the appropriate type or placed inside of an output statement for display.
int hisAge=getAge();
OR
System.out.println("Her age is: "+getAge());