Hello, There is the rule that primitive variables are passed by value into a method as an example with Circles shows: Circle c1 = new Circle (2,3,2); // Circle with x, y-Coordinates for center of the circle; // r for radius; -------------------------------------------------------- In this method "increase_rByValue1" is called and there are no changes made in the caller's objects due to pass by value for primitives:
c1.increase_rByValue1(c1.r) The methode is as follows: public void increase_rByValue1(double inc_r) {inc_r=inc_r+inc_r) The result is that the original value radius r of c1 is not beeing changed as primitive parameters are passed as a copy into the methode (pass by value). So a change in the parameter is not reflected in the calling method (object c remains untouched). -------------------------------------------------------------- Now the method has been changed that it is possible to CHANGE an instance VARIABLE: First method "increase_rByValue2" is called: c1.increase_rByValue2(c1.r) The methode is as follows: public void increase_rByValue2(double inc_r) {r=inc_r+inc_r) The result is that the original value radius r of c1 is beeing changed. The primitive parameter inc_r is passed as a copy into the methode (pass by value) but the copy is stored back to the instance which is referencing the method. So a change in the parameter is reflected in the calling method (calling object c1's instance variable r is changed. => There is an change in the callers object that is only visible if you check the called method. -------------------------------------------------------------- Now the second method "increase_rByValue3" is called: c1.increase_rByValue3(c1.r) The methode is as follows: public void increase_rByValue3(double inc_r) {r=inc_r+inc_r) The result is that the original value radius r of c1 is beeing changed. The change doesn't take place in the called method but in the caller itself as the changed and return copy of the original parameter is stored back to c1: => There is an change in the callers object that is all visible in the main prog. You needn't check the code of the called method: MY QUESTION IS: Which of the 2 methods to change a primitive parameter is the most widley used. When do you use the approach of method "increase_rByValue3" and when "increase_rByValue3" I appreciate your answers. Thomas
------------------
Thomas Markl
Ranch Hand
Joined: Mar 08, 2001
Posts: 192
posted
0
SORRY, There was a mistake in method "increase_rByValue3" so I post it correcty: -------------------------------------------------------------- Now the second method "increase_rByValue3" is called: c1.r = c1.increase_rByValue3(c1.r) The methode is as follows: public double increase_rByValue3(double inc_r) {inc_r=inc_r+inc_r; return inc_r) The result is that the original value radius r of c1 is beeing changed. The change doesn't take place in the called method but in the caller itself as the changed and return copy of the original parameter is stored back to c1.r
Dave Vick
Ranch Hand
Joined: May 10, 2001
Posts: 3244
posted
0
Thomas Because the method is an instance method of your Circle class you can just call it with an instance of a circle, there is no need to pass in the value of the radius. c1.increase_rByValue3(); because it is an instance method it will get all of the variables of the calling instance, so in the method you can jsut say this: public void increase_rByValue3(){ r *= 2; } This will change the variable r of the Circle object you called the method with. If there are to be multiple increase_r methods then you can overload them, like this:
There is no need to pass variable and values back and forth if you dont have to, it make the code less readable to have to try trace them back and forth and follow everything that happens to them. In my experience, which is not too extensive, most people do not use return from instance methods to change instance variables. They use one of the ways I showed above. The only time you would have to use return would be if you were changing the variable in a static method (that doesn't get the current object), or if the method performed some complex calculations or something of that nature, or if there were a series of method calls that were called by each other. Hope that helps you out Dave