When working with methods, there will be times that we want to keep variables only within our method. Java has a way of doing this through the usage of parameters. Parameters are declared within the parenthesis of our method. Let’s go over an example of a method with parameters.

In the example above, we have the user made method and Java’s main method. Within the printingMethod’s parentheses, we have the parameter String str
. This is the variable that we want to create and use solely within the printingMethod()
method.
By adding the parameter String str
, we now have a String variable that will only exist within the confines of the method. For example, if we were to try and print out String str in the main method, we would get an error message stating that it cannot find the symbol.
Also, by adding a parameter into our method, we make it necessary to put a String value into the method when it is called within the main method. Otherwise, our code will fail to compile.
In the code above, we have the String coffee
within the method when it is called. This can be referred to as an argument.
Also, it is important to note that you can pass as many parameters and arguments as you want. Just be sure to separate each separate value by a comma. Look at the example given below:

After compiling and running the code above, we get the following output:

To summarize, parameters are the variables that are declared within the parenthesis of a method and only visible within that method. Arguments are the input given when the method is called upon. Also, there is no limit to the amount of parameters/arguments you can add to your method.
Interviewers can often ask questions about the difference between parameters and arguments, as well as what they accomplish. If you are aspiring to make coding a career choice, it would behoove you to be able to know and explain both concepts.
Leave a Reply