Broad Network


Effects of Passing Primitive and Instantiated Objects to Java Methods

Java Just After the Basics – Part 3

Forward: In this part of the series, I talk about Effects of Passing Primitive and Instantiated Objects to Java Methods.

By: Chrysanthus Date Published: 2 Sep 2012

Introduction

This is part 3 of my series, Java Just After the Basics. In this part of the series, I talk about Effects of Passing Primitive and Instantiated Objects to Java Methods.

Passing Primitive Data Type Arguments
Examples of primitive data types are int and double. When you pass a primitive data type as variable to a method, you are passing a copy of the value (object). In the method, the parameter name identifiers the copy. You can do whatever you want to do (change it, print it, return it, etc.) with the copy in the method. At the end of the method, the value of the original (outside) variable is still maintained for the variable, but the parameter value might have changed. Read and try the following code:

class Illustration
    {
        void myMthd(int para)
            {
                para = 15;
                System.out.println(para);
            }
    }

class Primitive
    {
        public static void main(String[] args)
            {
                Illustration obj = new Illustration();
                int var = 10;
                obj.myMthd(var);

                System.out.println(var);

            }
    }

The variable, var is declared and assigned (initialized) in the main method. It is used as argument in the calling method. The called method receives but a copy of the value with the parameter, para. The parameter is changed and printed (displayed) in the called method. At the end of the main method, the value of var is printed, and you should note that the value of var has not changed, confirming that in the method definition (called method), it is a copy in a different variable (para) that exist. What I have said here applies to cases of more than one argument and corresponding parameters.

So, with primitive objects, it is copies of argument values that are sent and not the variables themselves.

Passing Instantiated Objects
When you pass an instantiated object to a method, you pass the actual object and not a copy. Any changes made to the parameter object in the method definition occurs at the object declared outside the method. So, in this case, the argument variable and the parameter variable, are referencing (referring to) the same object. Read and try the following code:

class AClass
    {
        int prop = 20;
    }

class Illustration
    {
        void myMthd(AClass para)
            {
                para.prop = 35;
                System.out.println(para.prop);
            }
    }

class Instantiating
    {
        public static void main(String[] args)
            {
                Illustration obj1 = new Illustration();
                AClass obj2 = new AClass();

                obj1.myMthd(obj2);

                System.out.println(obj2.prop);

            }
    }

There are two classes of interest (Aclass and Instantiating) in the code. In the main method, the two classes are instantiated. The object of AClass is sent as argument to the method of the object of Illustration. In the method definition of the Illustration class, the parameter, para receives the object reference, referencing the same object; you cannot see the reference. With the parameter variable, para, the value of the property of the object is changed and printed. Down as the last statement in main, the same property of the object identified by a different variable, is printed. The two printed values are the same, meaning that in the method definition (description) and outside the method definition, the parameter variable and the argument variable are referring to the same object.

In other words, the object inside the method definition and the corresponding instantiated object outside the method definition, are the same. This is the case for passing of instantiated objects.

Conclusion
When you pass a primitive object to a method, you pass a copy, while if you pass an instantiated object, you pass a reference to the same object and not a copy.

Let us take a break here. We continue in the next part of the series.

Chrys
NEXT

Related Articles

Java Course

Comments

Become the Writer's Fan
Send the Writer a Message