Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Why re-name these method parameters?

  1. #1
    Member
    Join Date
    Jul 2012
    Location
    Dallas area
    Posts
    34
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default 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
    void arraycpy(int[] destination , int[] source);
    i call it that way
    arraycpy(myArray,anotherArray);
    You call it
    arraycpy(bla1,bla2);
    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!

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Why re-name these method parameters?

    Quote Originally Posted by pict3000 View Post
    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:
    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 View Post
    ...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 View Post
    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:
    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:
    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:
    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!

  4. #4
    Member
    Join Date
    Jul 2012
    Location
    Dallas area
    Posts
    34
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Why re-name these method parameters?

    Quote Originally Posted by pict3000 View Post
    ... - 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 View Post
    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 View Post
    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:
    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;
    }

  7. #7
    Member
    Join Date
    Jul 2012
    Location
    Dallas area
    Posts
    34
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. pass parameters from jsp to another jsp
    By chathura in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: March 24th, 2012, 12:50 PM
  2. Calling method cant get the parameters right
    By frozen java in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 22nd, 2011, 02:23 PM
  3. Help using parameters
    By white97 in forum Object Oriented Programming
    Replies: 6
    Last Post: August 9th, 2011, 07:28 PM
  4. Can you pass parameters from one method into another method?
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: April 14th, 2011, 07:46 AM
  5. Actual and Formal Parameters
    By mikec2500 in forum Object Oriented Programming
    Replies: 4
    Last Post: February 10th, 2011, 08:48 AM