3. Method Components

3.1. Objectives

  • Describe what is meant by a method signature
  • Describe the parameter passing process
  • Implement a static method with a return type
  • Implement a static method without a return type

3.2. Time Goal

  • 25 minutes on this section

3.3. Key Terms

argument
A value that you provide when you call a method. This value must have the type that the method expects.
parameter
A piece of information that a method requires before it can run. Parameters are variables: they contain values and have types.
parameter passing
The process of assigning an argument value to a parameter variable.
invoke (call)
To cause the code in a method to execute. Also known as “calling” a method
local variables
Only exist within the method they are declared in
method signature
Part of the method declaration. It’s the combination of the method name and the parameter list.

3.4. Common Mistakes

  • “Declaring” arguments in a method invocation
  • For parameters, failing to declare the type of each variable separately

3.5. Exercises

Note

Some of the methods we have used require arguments, which are the values you provide in parentheses when you invoke the method.

  • Examples:

    • The Math.sin method takes a double argument. To find the sine of a number, you have to provide the number: Math.sin(0.0).
    • The System.out.println method takes a String argument. To display a message, you have to provide the message: System.out.println("Hello").

When you invoke a method, you provide the arguments. When you define a method, you name the parameters, which are variables that indicate what arguments are required. The following class shows an example:

The printTwice method has a parameter named s of type String. When you invoke printTwice, you have to provide an argument with type String.

You could think of arguments as the actual values (can be an expression) passed by a method call and that they are assigned to parameters and that parameters behave like variables.

Before the method executes, the arguments gets assigned to their corresponding parameter. In this example, the argument “Don’t make me say this twice!” gets assigned to the parameter s.

This process is called parameter passing because the value gets passed from outside the method to the inside. An argument can be any kind of expression, so if you have a String variable, you can use its value as an argument:

String message = "Never say never.";
printTwice(message);

The value you provide as an argument must have the same (or compatible) type as the parameter. For example, if you try:

printTwice(17);  // syntax error

You will get an error message like this:

File: Test.java  [line: 10]
Error: method printTwice in class Test cannot be applied
       to given types;
   required: java.lang.String
   found: int
   reason: actual argument int cannot be converted to
           java.lang.String by method invocation conversion

Sometimes Java can convert an argument from one type to another automatically. For example, Math.sqrt requires a double, but if you invoke Math.sqrt(25), the integer value 25 is automatically converted to the floating-point value 25.0. But in the case of printTwice, Java will not convert the integer 17 to a String.

Parameters and other variables only exist inside their own methods. Inside main, there is no such thing as s. If you try to use it there, you’ll get a compiler error. Similarly, inside printTwice there is no such thing as message. That variable belongs to main. Because variables only exist inside the methods where they are defined, they are often called local variables.

Note

Here is an example of a method that takes two parameters:

public static void printTime(int hour, int minute) {
System.out.print(hour);
System.out.print(":");
System.out.println(minute);
}

In the parameter list, it may be tempting to write:

public static void printTime(int hour, minute) {  // error

But that format (without the second int) is only allowed for local variables. For parameters, you need to declare the type of each variable separately.

To invoke this method, we have to provide two integers as arguments:

int hour = 11;
int minute = 59;
printTime(hour, minute);

Beginners sometimes make the mistake of “declaring” the arguments:

int hour = 11;
int minute = 59;
printTime(int hour, int minute);  // syntax error

That’s a syntax error, because the compiler sees int hour and int minute as variable declarations, not expressions. You wouldn’t declare the types of the arguments if they were simply integers:

printTime(int 11, int 59);  // syntax error

Pulling together the code fragments, here is the complete program:

Note

A method signuaure is the name of the method with its return type and parameters. An example of a signature is:

public static boolean isFull() //signature
//Rest of method code is not part of the signature

In this case the method returns a boolean value (either true or false) and takes no parameters.

Note

Run this code and use to answer the questions below.

    Q-96: Why won’t this code compile?
  • There isn't a return statement in the main method
  • Incorrect
  • There isn't a return statement in the calculateArea method
  • Correct
  • The calculateArea method needs to come before the main method
  • Incorrect
  • The method signature for calculateArea should be changed to public static void calculateArea(double radius)
  • Incorrect
  • The method signature for calculateArea should be changed to public static double calculateArea(radius)
  • Incorrect

Correct the code so that program now compiles. What is the output value when it is run?

    Q-97: Which of the following values could be returned from a method with the signature public static int numberAttendingEvent(boolean isRaining, float temperature)
  • 0
  • Correct
  • 100
  • Correct
  • true
  • Incorrect
  • 11.5
  • Incorrect
  • -10
  • Correct, the method signature seems to indicate the number of people who would attend an event. You can't have negative people, however, the method signature allows it to return any valid int including negative values.
Next Section - 4. Method Development