Google+

79. Argument Passing






There are two ways of passing arguments:
  1. Call-by-Value   - The value of the argument is passed to the parameters (values of the primitive data type are passed)
  2. Call-by-Reference  - The reference of the argument is passed to the parameters (objects are passed) 
Lets implement the call-by-value on Eclipse IDE:

1. Launch Eclipse IDE, Create a new Java Project 'Project 005', Create a Test class without main( ) method as shown below:



2. Create Test( ) constructor with two int type parameters as shown below and save:



3. Create another class 'CallByValue' with main( ) method as shown below



4. Declaring two integer variables 'i' and  'j' and initialize them with integer values as shown below:



5. Print the values of 'i' and 'j' before calling Test( ) constructor in Test Class as shown below:



6. Create an object 'object1' and pass the values of i,j to the Test( ) constructor in Test class as shown below:


7. Print the values of 'i' and 'j' after calling as shown below:



8. Save and Run the 'CallByValue' class 

9. Observe that the output is displayed in console as shown below:



What did we understand from the 'CallByValue' program ?

The operations that occurred on instance variables 'i' and 'j' inside the Test( ) constructor (i.e. i = i*2 and j = j/2 ) have no effect on the values of i and j in 'CallByValue' class. This has been proved as we've printed the values of 'i' and 'j' before and after calling the Test( ) constructor and we found that values of variables before and after printing is same.

How to solve this problem ?

This is possible by using the other way of passing the arguments i.e. Call-By-Reference (i.e. passing objects)

Download this Project:

Click here to download the project containing 'Test' and 'CallByValue' classes used in this post (You can download the project and import into Eclipse IDE on your machine)



Lets implement the call-by-reference on Eclipse IDE:

1. Launch Eclipse IDE, Create a new Java Project 'Project 006', Create a 'Test' class without main( ) method as shown below:



2. Declare two instance variables 'i' and 'j' as shown below:



3. Create Test( ) constructor that receives an object as shown below:



4. Create Test( ) constructor without parameters as shown below and save:



5. Create a class 'CallByReference' as shown below:



6. Create an object 'object1' as an instance of Test Class and initialize the instance variables accessed by the object1 from Test Class as shown below:



7. Print the instance variables 'i' and 'j' of object1 before passing the object1 to Test( ) constructor in Test class as shown below:



8. Create another object 'object2' and pass the 'object1' as argument while creating 'object2' as shown below:



9. Print the instance variables 'i' and 'j' of  object1 after passing the object1 as shown below:


10. Save and Run the 'CallByReference' class

11. Observe that the output is displayed in console as shown below:



What did we understand from the Call By Reference Program ?

When object is passed as a parameter and the instance variables 'i' and 'j' of 'object1' are changed (i.e. object1.i = object1.i * 2 and object1.j = object1.j /2 ) , on printing these values in 'CallByReference' class, the changed values of 'i' and 'j' variables of 'object1' are displayed.

Download this Project:

Click here to download the project containing 'Test' and 'CallByReference' classes used in this post (You can download this project and import into Eclipse IDE on your machine)




Please comment below to feedback or ask questions.

How to return objects will be explained in next post.



2 comments:

Krishna Kumar said...

Hi, First of all Appreciate all your efforts in making others to learn so easily.

Shouldnt the line be object2.i and object 2.j in Step number 9

Arun Motoori said...

@Krishna Kumar - There is no mistake in step number 9. I have correctly specified object1.i and object1.j. On printing object2.i and object2.j, value 0 will be printed as we have not initialized the values for object2.i and object2.j (we have only initialized the values for object1.i and object1.j).

Blog Index