Java Method Parameters


Parameters and Arguments

Information can be transmitted along paths as a parameter. The parameters act as a variable within the path.

The parameters are specified after the path name, inside brackets. You can add as many parameters as you want, just split them into commas.

The following example contains a method that takes a string called fname as a parameter. When the path is called, we transmit the first word, which is used within the path to print the full name:


Example
public class Main {
            static void myMethod(String fname) {
              System.out.println(fname + " Refsnes");
            }
          
            public static void main(String[] args) {
              myMethod("Liam");
              myMethod("Jenny");
              myMethod("Anja");
            }
          }
          // Liam Refsnes
          // Jenny Refsnes
          // Anja Refsnes
          

When a parameter is passed along the path, it is called an argument. So, from the example above: the fname parameter, while Liam, Jenny and Anja are arguments.



Multiple Parameters

You can have as many parameters as you like:


Example
public class Main {
            static void myMethod(String fname, int age) {
              System.out.println(fname + " is " + age);
            }
          
            public static void main(String[] args) {
              myMethod("Liam", 5);
              myMethod("Jenny", 8);
              myMethod("Anja", 31);
            }
          }
          
          // Liam is 5
          // Jenny is 8
          // Anja is 31

Note that if you are working with multiple parameters, the call method should have the same number of arguments as there are parameters, and the arguments should be conveyed in the same order.



Return Values

The void keyword, used in the examples above, indicates that the method should not return a value. If you are looking for a way to recover value, you can use the old data type (such as int, char, etc.) instead of void, and use the return keyword within the method:


Example
public class Main {
            static int myMethod(int x) {
              return 5 + x;
            }
          
            public static void main(String[] args) {
              System.out.println(myMethod(3));
            }
          }
          // Outputs 8 (5 + 3)
          

This example returns the sum of two parameters of the method:


Example
public class Main {
            static int myMethod(int x, int y) {
              return x + y;
            }
          
            public static void main(String[] args) {
              System.out.println(myMethod(5, 3));
            }
          }
          // Outputs 8 (5 + 3)
          

You can also save the result to the variable (recommended, as it is easy to read and save):


Example
public class Main {
            static int myMethod(int x, int y) {
              return x + y;
            }
          
            public static void main(String[] args) {
              int z = myMethod(5, 3);
              System.out.println(z);
            }
          }
          // Outputs 8 (5 + 3)
          


A Method with If...Else

Commonly used if ... else statements within methods:


Example
public class Main {

            // Create a checkAge() method with an integer variable called age
            static void checkAge(int age) {
          
              // If age is less than 18, print "access denied"
              if (age < 18) {
                System.out.println("Access denied - You are not old enough!");
          
              // If age is greater than, or equal to, 18, print "access granted"
              } else {
                System.out.println("Access granted - You are old enough!");
              }
          
            }
          
            public static void main(String[] args) {
              checkAge(20); // Call the checkAge method and pass along an age of 20
            }
          }
          
          // Outputs "Access granted - You are old enough!"