100 likes | 235 Vues
Passing objects to a method. It is both correct and common to pass objects to methods. package TimoSoft; public class Box { int a, b, c; void setAValue(int aVal) { a=aVal; } void setBValue(int bVal) { b=bVal; } void setCValue(int cVal)
E N D
Passing objects to a method • It is both correct and common to pass objects to methods. tMyn
package TimoSoft; public class Box { int a, b, c; void setAValue(int aVal) { a=aVal; } void setBValue(int bVal) { b=bVal; } void setCValue(int cVal) { c=cVal; } tMyn
Box(int aValue, int bValue, int cValue) { setAValue(aValue); setBValue(bValue); setCValue(cValue); } int getVolume() { return a*b*c; } boolean sameDimensions(Box obj) { if((obj.a==a) && (obj.b==b) && (obj.c==c)) return true; else return false; } tMyn
boolean sameVolume(Box obj) { if(obj.getVolume()==getVolume()) return true; else return false; } } tMyn
package TimoSoft; public class BoxTest { public static void main(String[] args) { Box first=new Box(10, 2, 5); Box second=new Box(10, 2, 5); Box third=new Box(4, 5, 5); System.out.println("first does have the same dimensions as second: "+ first.sameDimensions(second)); System.out.println("first does have the same dimensions as third: "+ first.sameDimensions(third)); System.out.println("first does have the same volume as third: "+ first.sameVolume(third)); } } tMyn
run: first does have the same dimensions as second: true first does have the same dimensions as third: false first does have the same volume as third: true BUILD SUCCESSFUL (total time: 0 seconds) tMyn
The methods sameDimensions() and samaVolume() compare the object passed as a parameter to the invoking object. • In both cases, notice that the parameter obj specifies Box as its type. • Although Box is a class type created by the program, it is used in the same way as Java’s built-in types. tMyn
There are two ways in which an argument can be passed to a method. • The first way is call-by-value. • This method copies the value of an argument into the formal parameter of the method. • Therefore, changes made to the parameter of the method have no effect on the argument in the call. • The second way an argument can be passed is call-by-reference. • In this method, a reference to an argument (not the value of the argument) is passed to the parameter. • Inside the method, this reference is used to access the actual argument specified in the call. tMyn
This means that changes made to the parameter will affect the argument used to call the method. • In Java, when you pass a simple type, such as int or double, to a method, it is passed by value. • Thus, what occurs to the parameter that receives the argument has no effect outside the method. • When you pass an object to a method, the situation changes dramatically, because objects are passed by reference. • Keep in mind, that when you create a variable of a class type, you are only creating a reference to an object. tMyn
Thus, when you pass this reference to a method, the parameter that receives it will refer to the same object as that referred to by the argument. • This effectively means that objects are passed to methods by the use of call-by-reference. • Changes to the object inside the method do affect the object used as an argument. • As a point of interest, when an object reference is passed to a method, the reference itself is passed by use of call-by-value. • However, since the value being passed refers to an object, the copy of that value will still refer to the same object referred to by its corresponding argument. tMyn