0

Basically same question as above, if an ArrayList is passed to a constructor of an object, is it passed by reference or value?

In the example below, once I create the TickHistory object and pass an arraylist to it, if I later change the arraylist that I passed, will it also change in the TickHistory object instance?

public class TickHistory {
    List<Tick> ticks;

    String period;
    String name;
    Integer year;


    public TickHistory(String name, String period, Integer year, List<Tick> ticks){
        this.name=name;
        this.period=period;
        this.year=year;
        this.ticks=ticks;
    }

}
LucasSeveryn
  • 5,984
  • 8
  • 38
  • 65
  • 2
    Have you simply tried it ? – Derlin Apr 12 '16 at 08:28
  • You want: this.ticks = new ArrayList<>(ticks); – Tatarize Apr 12 '16 at 08:31
  • yes, thanks. It's very confusing how they say 'pass-by-value' when in practice it doesn't pass by value like it would pass a primitive instead behaving as if reference to an object was passed. – LucasSeveryn Apr 12 '16 at 08:33
  • Note, the new array list will be full of the *same* objects. So if you modify one tick you modify that tick in both lists. But, the order of the objects in the arraylist are fine. – Tatarize Apr 12 '16 at 08:33
  • You can't pass an arraylist object at all because you can't get hold of it. (There's no dereference operator like `*` in C.) In your exampe your constructor accept a reference to an arraylist. So, a more sensible question ta ask is "are object references passed by referense or value?" and the answer is, by value, just like all other types in java – aioobe Apr 12 '16 at 08:34
  • They want to hide the pointers. They are there, but under the surface. You can't access or touch the pointers. But everything is passed by them. You never create a new object by simply calling a function. All objects must be declared new explicitly. – Tatarize Apr 12 '16 at 08:34
  • Basically suppose that everything was passed as &var, and then if you don't want to mess the stuff up then make a new object and put your stuff in that. In the limited thing that you want to pass your array to out but not let that thing that has your array dick with your array, you put it in an immutable version of that container. That or give them a new copy but that's kinda a waste of memory. – Tatarize Apr 12 '16 at 08:37
  • @aioobe looks like from practical perspective Java is never pass-by-value except when passing primitives. Sorry for confusion, I was only interested in how it works in practice, which I hoped my question explained, rather than being interested in the complexities of the language. – LucasSeveryn Apr 12 '16 at 08:48
  • I don't know what you mean by "from a practical perspective". The only thing you need to grasp is that a variable never contains an object. (What you get back from `new ArrayList` is a reference to the heap.) As soon as you fully understand that, everything falls into place, and it's nothing confusing at all with the fact that reference are passed by value, just like primitives. – aioobe Apr 12 '16 at 08:51

1 Answers1

0

Java is always pass-by-value. Unfortunately, they decided to call pointers references, thus confusing newbies. Because those references are passed by value.

Check this article

Also check these posts

Community
  • 1
  • 1
0x44656E6E79
  • 1,053
  • 3
  • 14
  • 21
  • ok so it means that if I change the arraylist that I passed, the one inside the object will not change? – LucasSeveryn Apr 12 '16 at 08:30
  • You want to declare a new arraylist and fill it with ticks. So new ArrayList<>(ticks); – Tatarize Apr 12 '16 at 08:32
  • maybe I'm worng here, but the arraylist you passed to the constructor is a different one than the arraylist within your object. Once you pass the arraylist (the values) to the constructor, the values will be written into the arraylist of your object (pass-by-value). Change the arraylist which was passed before, will not change the arraylist within your object. – 0x44656E6E79 Apr 12 '16 at 08:51