Southrop
Southrop

Reputation: 23

Don't you have to use the same parameter names when calling a method?

In the code below, I've written two methods:

I have the variable named test in userInputhere while the parameter in Hello is named message. In userInputhere I use test instead of message - why does this work?

Does the parameter not matter when you invoke the Hello method?

I get that when entering a method that returns something, it has to be defined and the parameters define further what the method is going to work on, so, I had assumed that, when calling said method from another method, you would have to use the same parameters, but that doesn't seem to be the case.

 import java.util.Scanner;
 public class methodsandparameters {
        static Scanner input = new Scanner(System.in);
        public static void main(String[] args){
            userInputhere();
        }

        public static void userInputhere(){
            String test = input.nextLine();
            System.out.println(Hello(test));
        }

        public static String Hello(String message){
            if (message.equalsIgnoreCase("Hi")) {
                return "Hello";
            } else {
                return "Goodbye";       
            }
        }
}

Upvotes: 0

Views: 1376

Answers (3)

Bernhard Barker
Bernhard Barker

Reputation: 55649

The name of a variable only matters within it's scope (i.e. where you can reference it)[1].

For a method parameter, that's only in the body of that method.

Outside of that method, the only thing that matters is the type - String, in this case. As long as you pass it something of the same type, or a type that can be automatically converted to the given type, it's happy - whatever you called the parameter (message, in this case) will be given the value of whatever you passed to your function in that position in the arguments and each time you use that parameter name in the method, you'll be using that value that's been given to it (at least until you reassign it in your method, by saying message = ...).

You can also do things like Hello(input.nextLine()) or Hello("Hi") - no need to use a temporary variable.


[1]: Until you start talking about reflection (but no need to worry about that yet).

Upvotes: 1

depperm
depperm

Reputation: 10756

The reason the parameter names, as you call them, don't match is only one is a parameter. When you call a function you pass it a number of arguments that must match the type and number of parameters a function takes. So if you have a function add(int x, int y) it doesn't matter how you call it as long as both arguments are ints, they don't have to be named x and y when you call the function.

So a function/method takes parameters and calling a function passes arguments.

Upvotes: 1

user2121620
user2121620

Reputation: 676

When you invoke the Hello method, its parameter (message) is contained inside of the Hello method itself. You are allowed to pass any argument to Hello as long as it is a string (since message is of type String). It does not matter what you pass to Hello as long as it resolves to a String. So in your example, the variable test works fine because it is a String.

The program is not actually invoking "test" or invoking "message", it is only invoking the Hello method which can take any String as an argument.

Upvotes: 1

Related Questions