Difference between revisions of "Calling a User-Defined Method"

From CompSciWiki
Jump to: navigation, search
(Method Calling)
(Method Calling)
Line 1: Line 1:
 +
{{1010Topic|Chapter_TOC=[[User-Defined_Methods]]|Introduction=Now that you've written your first method, this section will show you how to use it. When you want to execute a method which you have written, it's refered to "calling" the method. When a method is called, it executes a relationship of 2 or more methods. The method who wants the work done, and another method which performs the work.|Overview=This topic will describe how to execute/call your own methods.}}
 +
 
==Method Calling==
 
==Method Calling==
  
Line 57: Line 59:
  
  
Code Example:
+
==Code Example:==
  
 
<pre>
 
<pre>

Revision as of 15:38, 20 March 2007

COMP 1010 Home > User-Defined_Methods


Introduction

Now that you've written your first method, this section will show you how to use it. When you want to execute a method which you have written, it's refered to "calling" the method. When a method is called, it executes a relationship of 2 or more methods. The method who wants the work done, and another method which performs the work.

   

{{{Body}}}

Method Calling

Two methods are involved in every method call:

  • Called method - the method to execute
  • Calling method - the method that does the calling

To call a method:

  1. Enter the name of the class containing the method, if the calling method is from a different class.
  2. Enter the name of the method followed by the character (.
  3. For each parameter in the called method, enter a variable or value of the same type as the parameter.
  4. Enter the character ).

NOTE: If the called method has no parameters, you will still need to follow the method name with parentheses.

If the called method has a return value, make sure to call the method in a place where the return value is used. Usually you will save the return value in a variable, then use the variable later.

Example:

int sum;

// The sum of firstNum and secNum will be saved in the sum variable
sum = calculateSum(firstNum, secNum);
System.out.println(sum);


  • If the called method and the calling method are in the same class, use the name of the method along with the parameters in parentheses.
Example:
methodName(firstNum, secNum);


  • If the called method is in a different class than the calling method, write the name of the class before the method name, separated by a period, then the parameters following.

Example:

OtherClass.methodName(firstNum, secNum);

When you attempt to call a method without stating the class name first, Java will search for the method in the same class that is being run. However, Java will not search other classes for the method, so you will need to tell Java which class contains the method if you did not declare the method in the running class.

Consider the following code which you may be familiar with:

int maximum = Math.max(firstValue, secondValue);

In this statement, you are telling Java to search the Math class for a method called max.


  • To call a method which has no parameters, simply follow the method name with empty parentheses.

Example:

methodName()


Code Example:


public class myClass 
{

	public static void main(String[] args)
	{
		int first = 23;
		int second = 68;
		int great;

		great = getGreaterInt(first, second);
		System.out.println("Greater value of " + first + " and " + second + ": " + great);
	}
  
  
	public static int getGreaterInt( int value1, int value2 )
	{
		int biggest; //Declare a variable to hold the biggest int

		if ( value1 > value )
		{
			biggest = value1;
		}
		else
		{
			biggest = value2;
		}

		return biggest;
	}
 
}