2

I have created a List and added 2 elements to it. I pass this List to a method and initialized the list to new ArrayList().

When the method returns I print the size of list but it still shows 2.

public class ValueChange {
    public void fun(List<Integer> l1){
        l1=new ArrayList<Integer>();
    }

    public static void main(String[] args){
        List<Integer> l1=new ArrayList<Integer>();
        l1.add(5);
        l1.add(6);
        System.out.println("Before = "+l1.size());
        ValueChange vc=new ValueChange();
        vc.fun(l1);
        System.out.println("After = "+l1.size());
    }
}
Chilledrat
  • 2,593
  • 3
  • 28
  • 38
TheUser
  • 129
  • 1
  • 1
  • 10
  • you are making a copy of l1 in the vc instance when you run `l1=new ArrayList();`, try changing that to `l1.add(7);`, I believe l1 in main() will change – Michael Nov 21 '13 at 17:18

4 Answers4

7

The line:

l1=new ArrayList<Integer>();

in your fun method will not actually change the list outside of this method scope. You can change the values inside them, but not reassign a new list to your arguments.

This actually a frequent error in java. If you want to know how the parameters are passed in a method, please read this post.

Community
  • 1
  • 1
benjamin.d
  • 2,801
  • 3
  • 23
  • 35
  • Hi Benjamin, Thanks for your reply. But still i am not getting it clear that if inside fun i add a new element to list it gets reflected but even when i set the reference to Null or initialize it to new ArrayList, it doesn't reflect. Ideally if it is pass by value no change should be reflected when control comes out of Fun method and if pass by reference then changes should be present even after method is exit. This mix of both feature confused me too much. could you explain a bit in simple way. – TheUser Nov 22 '13 at 05:18
3

Java uses pass by value of reference. Its like you are passing the reference as value. What you have done in the invoked method is you have changed the value of the formal parameter by assigning a new memory to it, It wont reflect in the actual parameter

Freaky Thommi
  • 756
  • 7
  • 18
1

That won't work because of the scope of the parameter.

You want to clear the list:

public void fun(List<Integer> l1){
    l1.clear();
}

As other answers have pointed out. Java's pass-by-value is what's causing your issue here. See THIS PAGE for explanation of this.

Dan Temple
  • 2,736
  • 2
  • 22
  • 39
1

Java is pass by value but it passes the value of the reference. This basically means that if you reassign a method parameter it does not get reflected in the original object.

Java makes a copy of the original reference when you pass it to a method so if you assign a new value to that reference you won't see the changes outside.

Both references are pointing to the same objet though.

Adam Arold
  • 29,285
  • 22
  • 112
  • 207
  • Can you add a reference about this *pass-by-reference-value*? – Luiggi Mendoza Nov 21 '13 at 17:22
  • This is not an official term. I just came up with it because it fits on the model. People often confuse this. See: http://stackoverflow.com/questions/40480/is-java-pass-by-reference – Adam Arold Nov 21 '13 at 17:24
  • 1
    I guessed that, that's why I asked for a reference on it. Then, please do not use informal terminology that can mislead readers of your answer. – Luiggi Mendoza Nov 21 '13 at 17:25