The Format of a Method

   

Java™ 2 Primer Plus
By Steven Haines, Steve Potts

Table of Contents
Chapter 5.  Methods


The general form of a Java method is

 return_type method_name( parameter_list )  {      declarations and statements...  } 

The method_name is any valid identifier and has the same naming rules as variables.

The return_type is the data type of the result returned from the method to the caller; if your method was defined to square an int, note that the method returns an int here.

The parameter_list is a comma-separated list containing the declarations of values passed to the method. Again, if you were writing a method to square an integer you would need to specify that the method receives one int and give it a name that you can reference from within the method.

The method name, return type, and parameter list define what is referred to as the method's signature: A method signature uniquely identifies a method.

Enclosed between the parentheses is the body of the method; this is where all the work takes place. The parentheses denote a block of code; variables can be declared in a block and blocks can contain other blocks. When the method has completed all the work it has to do, there are three ways it can return control back to the caller:

  1. Control passes back at the closing brace of the method if the method does not return a result.

  2. Manually return with no value:

     return; 

  3. Return a value:

     return expression; 

Note

graphics/01icon18.gif

Note that for a method to return without passing a result back to the caller, the method must be declared with a return type of void. void specifies that the method will not return any value.


The following code snippet shows a method that squares an integer:

 public static int square( int i ) {     return i*i;  } 

The square method is publicly available to anyone who wants to call it (described in the next section). It is static (similar to the main method you are already used to) meaning that there is only one instance of the method for all classes (discussed in the next chapter). The square method returns an int and accepts as its parameter list one int that is labeled i. The body of the method simply returns i*i, or the square of i.

The breakdown of the method components for the square method follow:

  • Method name: square

  • Return Type: int

  • Parameter List: int i

  • Return Expression: i*i

  • Return Statement: return i*i

The signature of the method, which uniquely identifies it, is

 public static int square( int i ) 

In this class there can only be one method named square that accepts a single int as its parameter list.

The body of the method is

 return i*i; 

Many of the terms described here will make more sense in the next chapter, but they are needed to make the example in the next section work.


       
    Top
     



    Java 2 Primer Plus
    Java 2 Primer Plus
    ISBN: 0672324156
    EAN: 2147483647
    Year: 2001
    Pages: 332

    Similar book on Amazon

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