Why re-name these method parameters?
I have a Java textbook example that is confusing to me because the 3 parameters in a called method have their names inexplicably changed when the method is created, and I can't figure out why. The specifics are as follows:
A reference variable named entity has been instantiated in the main method like this: Growth entity = new Growth();
Then it is initialized in the main method like this: entity.initialize(startSize, endSize, fractionGrowthRate);
All 3 of those parameter names exactly match variable names initialized just below the main method heading. Then here's where I start to be confused. In the driven class Growth (where the initialize method is made), the "initialize" method looks like this:
//************************************************** **
public void initialize(double start, double end, double factor
{
this.startSize = start;
this.endSize = end;
this fractionGrowthRate = factor;
} // end initialize
//************************************************** **
So why did they convert the parameter names?
Further confusing me is the fact that those same named 3 variables are declared just below the Growth class heading like this:
private double startSize; // initial size
private double endSize; // maximum size
private double fractionGrowthrate; // per unit size
Why convert those names when "start", "end", and "factor" never again appear anywhere else in the program?
Re: Why re-name these method parameters?
1-you know that this is legal and something that people do
2-it is done in such cases to help the reader in quickly understanding which are are the fields of the class and which are the arguments of the function.In this function this. is used,but when it does not,then how would you know which is the field and which is the argument.Moreover i do not remember if the compiler accepts such things.
3-More general,so more significant.A function can be called with MANY arguments.However it can not have the exact names of the arguments at every call,especially in a large program.Consider this function that copies two int arrays
Code :
void arraycpy(int[] destination , int[] source);
i call it that way
Code :
arraycpy(myArray,anotherArray);
You call it
I do not see why the function's implementation should use your's,mine or anyone's else names.It should use what it is more helpful in order the programmer to understand quickly what's happening.Imagine the arraycpy function to had as arguments array1 and array2,then you would not know what is the destination and what the source!
Re: Why re-name these method parameters?
Quote:
Originally Posted by
pict3000
I have a Java textbook example that is confusing to me because the 3 parameters in a called method have their names inexplicably changed......
When you call a method, you have to supply the correct types. If the method is defined as:
Code java:
public void initialize(double start, double end, double factor) {
//....
}
..this only means you have to send three doubles. The names in the method are the names to be used within the method, and stand alone from the names of the variables you may use when you use the method. I think the book you are using is trying to point this out. Just because the method calls the variables start, end, and factor, does not mean you have to supply variables of the same name, you only have to match the types.
Quote:
Originally Posted by
pict3000
...So why did they convert the parameter names?
again, they are not converted. The method only gets the values stored in the variables you supply when you call the method, regardless of the variable names.
Quote:
Originally Posted by
pict3000
Further confusing me is the fact that those same named 3 variables are declared just below the Growth class heading like this:
private double startSize; // initial size
private double endSize; // maximum size
private double fractionGrowthrate; // per unit size
Why convert those names when "start", "end", and "factor" never again appear anywhere else in the program?
again this is the same thing. The variable names used in the definition of the method initialize, are only for use within the method block. The instance variables that belong to each object, when an instance of the class is created, are there for the life of the class. The variables are not the same, and therefore can have different names.
You could rename your variables as follows:
Code java:
public void initialize(double x, double y, double z) {
//...
}
...however variable names x y and z do not offer much input as to their use within the method itself. The reader of your code would be better off with variable names like start end and factor. The variable names in your initialize method could be the same as the instance variables of the class. This is pretty common, but not required. Some code as an example:
Code java:
public class Test {
/* your init method */
public void initialize(double startSize, double endSize, double fractionGrowthrate) {//Names declared here...
//...are usable here...
this.startSize = startSize;
this.endSize = endSize;
this.fractionGrowthrate = fractionGrowthrate;
}//...but are no longer usable here.
public void someOtherMethod() {
if(startSize < 0) {
System.out.println("Start size is too small!");
}
}
/* Private instance variables */
//these variables are usable throughout the entire class.
private double startSize;
private double endSize;
private double fractionGrowthrate;
}
If you notice within the initialize method, the method variable names shadow out the instance variable names. This is because the compiler looks in the current scope for a matching variable name, and finds the variable names of the method parameters. In the someOtherMethod method, the compiler looks inside the current scope, and does not find a matching variable name. The next step for the compiler is to jump up one bracket, the next larger scope, (which in this case is the scope of the class), and finds the instance variable with a matching name. Therefore inside the someOtherMethod method, you are not required to use this.startSize, but in the initialize method, when you want to refer to the instance variables, you are required to. If you change the names of the method variables, you would not have to. The code:
Code java:
public class Test {
public void initialize(double start, double end, double factor) {
startSize = start;
endSize = end;
fractionGrowthrate = factor;
}
/* Private instance variables */
private double startSize;
private double endSize;
private double fractionGrowthrate;
}
These two classes do the same thing, and hopefully you can see how the variable names work through their use. Feel free to ask more questions!
Re: Why re-name these method parameters?
First to Samaras, then jps.
Many thanks, Samaras. Very helpful!
jps, now I get it. Seriously, a trillion thanks! And yes, I do have another question:
Your final example at the bottom...
I don't understand why my textbook uses a "this." reference before startSize, endSize, and fractionGrowthRate, because that contradicts its own advice - which is what I think you are saying - that the "this." reference is only necessary to differentiate between two like variable names on either side of the equals sign - like this:
this.startSize = startSize;
this.endSize = endSize;
this.fractionGrowthRate = fractionGrowthRate;
That's how the compiler would differentiate between instance variable and parameter - which I think is what you were saying (right?).
So in your example:
startSize = start;
endSize = end;
fractionGrowthRate = factor;
I believe what your saying is that the left side of the equals sign does NOT need the "this." references because the variables on either side of the equal sign are already different anyway. Am I following you on that?
Re: Why re-name these method parameters?
Yes, this. is not required if the object variable is not being hidden. Depending on your preference though, it may still be used. I've seen it done both ways.
Re: Why re-name these method parameters?
Quote:
Originally Posted by
pict3000
... - that the "this." reference is only necessary to differentiate between two like variable names on either side of the equals sign - like this:
this.startSize = startSize;
this.endSize = endSize;
this.fractionGrowthRate = fractionGrowthRate;
Not exactly, but yes. It is not about either side of, or the = mark. See below.
Quote:
Originally Posted by
pict3000
That's how the compiler would differentiate between instance variable and parameter - which I think is what you were saying (right?).
Those are the words for it.
Quote:
Originally Posted by
pict3000
So in your example:
startSize = start;
endSize = end;
fractionGrowthRate = factor;
I believe what your saying is that the left side of the equals sign does NOT need the "this." references because the variables on either side of the equal sign are already different anyway. Am I following you on that?
Yes and no. Lets take a larger sample for clarity:
Code java:
public class Test {
public void initializeIncorrect(double start, double end, double factor, double startSize, double endSize, double fractionGrowthrate) {
//Even though the variable names are not the same on both sides of the = mark,
//these parameter variables are still first in line because they are declared in the current scope
startSize = start;
endSize = end;
fractionGrowthrate = factor;
}
public void initializeCorrect(double start, double end, double factor, double startSize, double endSize, double fractionGrowthrate) {
//Here using this.variable says, jump out of the current scope, and grab the instance variables of this class without looking anywhere else
this.startSize = start;
this.endSize = end;
this.fractionGrowthrate = factor;
//If we tried the following line we would get an error:
this.start = 2.0;
//The error is because this. says look for instance variable and ignores the parameter.
}
/* Private instance variables */
private double startSize;
private double endSize;
private double fractionGrowthrate;
}
Re: Why re-name these method parameters?
Very thorough and informative, jps! That really helped a lot. Thank you so much.
And thanks to you, as well, helloworld922. I will watch my posting p's and q's.