Skip to main content
Engineering LibreTexts

12.6: Multiple Parameters

  • Page ID
    15233
  • 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) {
        ...
    

    But that format (without the second int) is only legal for variable declarations. In parameter lists, you need to specify 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);
    

    A common error is to declare the types of the arguments, like this:

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

    That’s a syntax error; 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
    
    • Was this article helpful?