-1

I have read this statement:

"A parameter is used as a sort of temporary messenger, carrying data originating from outside the constructor or method and making it available inside it."

So, does that mean that values of parameters are stored at first in the constructors and then passed to fields?

I still didn't understand the meaning of that. Any good explanation?

4 Answers 4

3

So when you see a constructor:

Foo(int x){ this._x = x; }

The parameter x is a temporary, local variable that contains the value passed into the constructor (or any other function). You then assign it to another variable this._x but you don't have to.

They're not stored in the constructor per se, but they are a temporary local variable that exists for the duration of the function's execution.

0
public class Whatever {
    public static int foo(int a){
        return a+2;
    }

    public static void main(String[] args){
        int x = 1;
        System.out.println(Whatever.foo(x));
    }
}

"Parameter" is "a". It is a name that exists only in local scope (scope is part of code where local variables are defined - that is simplification). Even though you call real value "x", you can take it's value from "a" in foo, because you passed this value in PARAMETER. This is the meaning of this sentence.

Btw: where did you read that? I'd like to append that to my "never read this stuff, it confuses you" list.

0

Google's definition on the word "parameter":

a numerical or other measurable factor forming one of a set that defines a system or sets the conditions of its operation.

I think it's better for you to go with this definition. The method that you are invoking is the "operation", and the definition of the method is the system. Your parameters are the "measurable factor" that the operation operate around.

Also as general advice, even when you are reading technical books, accept the definition of the dictionary before what's on the book.

0

A parameter is an argument passed to a function or method (depending on the language, that allows for outside values to be copied or used by a given function or object.

For example:

Let's say that there's an object that's an array and it has a method called "addTwo". This method takes two indices and adds the values at those indices. But how does the array know what two indices it needs? This is where parameters come in.

int[] exampleArray = {1,2,3,4};

exampleSum = exampleArray.addTwo(0,3);

exampleSum is now equal to 5 since 1+4 = 5, those being the values at indices 0 and 3 respectively.

Parameters allow you to work with non-global variables that are generated outside the method, effectively copying them inside and then doing something with them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.