Java Object as Parameter

Till yet, we have only been using the simple types as parameters to methods. However, it is both correct and common to pass objects to methods. For instance, look at the following short Java program :

Java Use Object as Parameter Example

Here is an example program, demonstrates, how to use object as method parameter in Java:

/* Java Program Example - Java Use Objects as Parameters
 * In Java, Objects may be passed to methods
 */

class Test
{
    int a, b;
    
    Test(int i, int j)
    {
        a = i;
        b = j;
    }
    
    /* return true if o is equal to invoking object */
    boolean equalTo(Test o)
    {
        if(o.a == a && o.b == b)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

public class JavaProgram
{   
    public static void main(String args[])
    {
        
        Test obj1 = new Test(100, 22);
        Test obj2 = new Test(100, 22);
        Test obj3 = new Test(-1, -1);
        
        System.out.println("obj1 == obj2 : " + obj1.equalTo(obj2));
        System.out.println("obj1 == obj3 : " + obj1.equalTo(obj3));
        
    }
}

When the above Java program is compile and executed, it will produce the following output:

java object as parameter

As you can see, the equalTo() method within Test compares two objects for equality and returns the result i.e., it compares the invoking objects with the one that it is passed. If they holds the same values, then the method returns true, otherwise false.

Notice that the parameter o in method named equalTo(), specifies Test as its type. Although Test is a class type created by the program, it is used in simply the same way as Java's built-in types.

One of the most common uses of the object parameters involves constructors. Often, you will want to construct a new object so that it is initially the same as some existing object. To perform this, you must define a constructor that takes an object of its class as a parameter. For example, here this version of the Box allows one object to initialize another :

/* Java Program Example - Java Use Objects as Parameters
 * In this program, Box allows one object to
 * initialize another 
 */

class Box
{
    double width;
    double height;
    double depth;
    
    /* notice this constructor, it takes an object of the type Box */
     Box(Box ob)        // pass object to the constructor
     {
         width = ob.width;
         height = ob.height;
         depth = ob.depth;
     }
     
     /* constructor used when all the dimensions specified */
     Box(double wid, double hei, double dep)
     {
         width = wid;
         height = hei;
         depth = dep;
     }
     
     /* constructor used when no dimensions specified */
     Box()
     {
         width = -1;         // use -1 to indicate
         height = -1;        // an uninitialized
         depth = -1;         // box
     }
     
     /* constructor used when cube is created */
     Box(double len)
     {
         width = height = depth = len;
     }
     
     /* compute and return the volume */
     double volume()
     {
         return width * height * depth;
     }
}

class Overload
{
    public static void main(String args[])
    {
        
        /* create boxes using the various constructors */
        Box mybox1 = new Box(100, 200, 150);
        Box mybox2 = new Box();
        Box mycube = new Box(7);
        
        Box myclone = new Box(mybox1);     // create a copy of mybox1
        
        double vol;
        
        /* get the volume of the first box */
        vol = mybox1.volume();
        
        /* print the volume of the first box */
        System.out.println("Volume of mybox1 is " + vol);
        
        /* get the volume of the second box */
        vol = mybox2.volume();
        
        /* print the volume of the second box */
        System.out.println("Volume of mybox2 is " + vol);
        
        /* get the volume of the cube */
        vol = mycube.volume();
        
        /* print the volume of the cube */
        System.out.println("Volume of cube is " + vol);
        
        /* get the volume of the clone */
        vol = myclone.volume();
        
        /* print the volume of the clone */
        System.out.println("Volume of clone is " + vol);
        
    }
}

When the above Java program is compile and executed, it will produce the following output:

object as parameter in java

As you will see, when you begin to create your own classes, giving many forms of the constructors is usually required to allow to be constructed in a convenient and efficient manner.

Java Online Test


« Previous Tutorial Next Tutorial »