Google+

78. Using 'objects' as parameters






So far we've only been using simple types like int, double etc as parameters in methods and constructors.But we can  pass objects to methods & constructors and also use objects as parameters in the methods & constructors.

The following example statements will show how to pass objects to methods & constructors :

Example: Passing Object 'box2' to Box( ) constructor in Box Class

Box box1 = new Box(box2);
  •    box1 and box2 are objects of Box class
  •    Here in this example we're passing the box2 object to Box( Box object) constructor in Box class 
Example: Passing Object 'box2' to equal( ) method in Box Class

boolean x =  box1.equal(box2);
  • box1 and box2 are objects of Box class
  • x is the boolean type variable which can store either true or false
  • equal( )  is a method of Box class which has the object parameter i.e. public boolean equal(Box object)
Lets implement 'passing the object to a constructor' on Eclipse IDE

1. Launch Eclipse IDE, Create a new Project 'Project 004' , Create a class called 'Box' and declare 'width', 'height' and 'depth' as instance variables as shown below:



2. Create Box( ) constructor which has three parameters to receive width, height and depth of a box as shown below:



3. Create Box( ) constructor which can receive object as parameter as shown below:



4. Create volume( ) method to calculate the volume of a box using the dimensions passed as argument variables or objects as shown below and save:



5. Create another class 'PassingObjects' with main( ) method as shown below:



6. Create box1 object as an instance of 'Box' Class and pass the dimensions width, height and depth in the object creation statement itself as shown below:



7. Create box2 object as an instance of 'Box' class and pass the object 'box1'  in the 'box2' object creation statement as shown below:



8. Call volume( ) method of Box class  and print the retrieved volume of the box1 and box2 as shown below:



9.Save and Run the 'PassingObjects' class as a Java Application

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



We have to understand that box2 object is nothing but a clone of box1 object, as we've passed box1 object  to  Box(Box object) constructor of Box class while creating box2 object.

Download this Project:

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

Lets implement 'passing the object to a method' on Eclipse IDE

Before starting it, I want to tell you that the method that receives object as parameter in this project is going to compare two objects.

1. Launch Eclipse IDE, Create a new Java Project 'Sixth Project', Create a package 'sixth_package', Create a class called 'Test' under 'sixth_package' package of 'Sixth Project' project as shown below:



2. Declare the instance variables 'a' and 'b' as int type in 'Test' class as shown below:



3. Create a Test( ) constructor that receives two integer values and assigns them to instance variables 'a' and 'b' of  'Test' class as shown below:



4. Create equals( ) method that receives object as a parameter and compares the instance variables of the received object with the instance variables of the 'Test' class as shown below and save:



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



6. Creates objects 'object1', 'object2' and 'object3' , and  pass the values of objects instance variables to the 'Test' constructor as shown below:


7. Compare two objects and print the result returned by the equals( ) method as shown below:



8. Save and Run the 'PassObjects' class as shown below:



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



Download this Project:

Click here to download the project containing 'Test' and 'PassObjects' 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.

Two ways of argument passing topic is explained in the next post.


No comments:

Followers :)

Blog Index