1 / 10

Passing objects to a method

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)

barr
Download Presentation

Passing objects to a method

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Passing objects to a method • It is both correct and common to pass objects to methods. tMyn

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

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

  4. boolean sameVolume(Box obj) { if(obj.getVolume()==getVolume()) return true; else return false; } } tMyn

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

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

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

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

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

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

More Related