Home > Java Basics > Passing Variables into Methods- Java Passing Mechanism Explained

Passing Variables into Methods- Java Passing Mechanism Explained

Every Programming Language has its own way of passing the variables into methods. There are basically two ways of passing variables- Pass by Reference and Pass By Value. The former deals with the passing of reference or pointer to particular variable and the latter involves passing a copy of the variable to the method. C supports both ways of passing and the Pass By Reference is supported b means of Pointers. Every Java Beginner ponders over “Is Java a Pass By Value?” or “Is it a Pass By reference?”  or “Both?”. Before answering the question i would like to throw light on Passing Object Reference variables and Passing primitive reference variables.

Object Reference Variables: These are the variables which refer to the object of the declared type or its subtype. Something like

Animal a = new Animal()

where  a is the reference variable

Passing Object Reference Variables:

When a object variable is passed into the method, only the copy of the Object reference is passed and not the Object itself. The bit pattern in the reference variable is copied into the method parameter. What is this bit pattern? This bit pattern is the address of the specific object in the memory (on the heap). In other words, both the caller and the called method will now have identical copies of the reference and thus both will refer to the same exact object on the heap.

Code Example:

import java.awt.Dimension;
class PassObjectRef

public static void main(String[] args)

Dimension plotDimension = new Dimension(20,30);
PassObjectRef passObj=new PassObjectRef();
System.out.println("B4 Updating: "+"H: "+plotDimension.height+" W:"+plotDimension.width);
//Before the object values are altered
passObj.updateDimension(plotDimension );
System.out.println("After Updating: "+"H:"+plotDimension.height+" W:"+plotDimension.width);
//After the object values are altered

void updateDimension(Dimension dim)

dim.height+=10; //Notice the values being changed here.




B4 Updating:  H: 30 W: 20
After Updating:  H: 40 W: 40

Notice that the values being updated in the updateDimension() method are being also reflected outside the scope of the method. That means both the plotDimension and dim object references are referring to the same object on the heap.

Passing Primitive Variables:
When a primitive variable is passed the value with in the variable is copied into the method parameter, which is nothing but the pass-by-copy-of-the-bits-in-the-variable.

Code Snippet:

class PassPrimVar

public static void main(String[] args)
int primitiveVariable =4;
PassPrimVar passObj =new PassPrimVar();
System.out.println</span>("Before Updating Value: "+primitiveVariable );
// Value being printed before changing
System.ou.println("After Updating Value: "+primitiveVariable );
// Value being printed after changing. Notice there is no difference

void updateValue(int var)

var+=40; //Value being Changed Here
System.out.println("While Updating Value: "+var);


Before Updating Value: 4
While Updating Value: 44
After Updating Value: 4

Notice that the Value is same before and after the updateValue() method has been executed. But in the method the value has been changed. In other words the value being changed in the method is not being reflected in the main() method, its local to the scope of the method in which it is changed.

Does Java Use Pass-By-Value?

Here is the main confusion- Does Java Use Pass By Reference for Object Reference Passing and Pass By Value for Primitive Variable Passing? Java is actually pass-by-value for all variables running within a single VM. Pass By Variable is nothing but passing the variable value. It is same even if you pass and object reference variable or an primitive variable, you are always passing the copy of the bits in the variable. So for passing Object reference variable you are passing the copy of bits which actually is the reference (or address) to the object on the heap and this results in both the reference variables referring to the same object. Because two identical reference variables refer to the exact same object, if the called method modifies the object, the caller will see that the object the caller’s original variable refers to has also been changed. But keep in mind- Suppose in the called method the object reference is reassigned a new object, the caller’s original variable will still refer to the same object on the heap. In this case there will be two references and two objects on the heap. See the code excerpt below:

void do()

Animal animal = new Animal(”Dog”);
//New Animal object created on the heap with name Dog referenced by animal
kill(animal); // animal being passed to the kill() method

void kill(Animal ani)

Method receives a copy of the reference, but animal and ani both still refer to same object
ani =new Animal(”Cat”);
But after this statement, the animal reference still refers to the object by name “Dog”,
but the ani reference now refers to the object by name “Cat”.
There are 2 references referring to 2 different objects on the heap.


Coming to the passing of primitive variables it is pretty simple and straight forward. When you pass the primitvie variable into a method, you’re passing a copy of the bits representing the value. For example if you pass an int variable with the value 6, and try to change the value in the called method, we can change it but the modifications will not be reflected in the caller’s vairable.

So the bottom line is- Java always uses Pass By Value– Be it for passing Object Reference Variable or primitive variables.

  1. July 3, 2008 at 8:36 pm

    Useful Post.
    Nice to see the Syntax Highlighter on your blog. Makes the code snippets a lot readable.

  2. July 3, 2008 at 8:39 pm

    Thanks!! I need to thank Balaji Rao for the input 🙂
    But need to do something for the Indentation

  3. July 3, 2008 at 10:09 pm

    The comparison of java and C++ needs to be done carefully because they have many things in common, but some starange uncommon things as well. Personaly it will be good if you dont intermix the discussion, as that is harder to grasp!
    Over all a very nice post buddy,
    keep it up and thanks for the links at the end to my blog. 🙂

    Saurabh Patil

  4. July 8, 2008 at 10:19 pm

    Good job, you provided some nice information

  5. July 9, 2008 at 12:16 am

    Yannick van den Bos,
    Thanks!! 🙂 Suggestions are always welcome.
    You can suggest here- https://javaforyou.wordpress.com/suggest-a-post/

  6. March 23, 2009 at 7:31 am

    Can you make your dog and cat example more plain and simple..don’t understand??

  7. April 3, 2009 at 12:57 am


    I have tried to be as simple as possible. If you be more specific then i can help you out.

  8. Andrew
    May 29, 2010 at 8:54 pm

    So tell me how would you change primitave varibles? If its only passed by value how can i posiblly change the actual varible i’m passing? This is whats confusing. Is it no possible to change primitve types in methods?

    • June 14, 2010 at 10:38 pm

      Why do u want to change the variables? Thats a bad practice to change the value of the variables within the methods. One should try to make the methods hav no side effects. So u can return the value and assign to the variable u want to change. Or u can make the variables as fields of the class, create a instance of that class and then pass it to the method. In the method u can update the values of the fields of the class

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: