-1

How is the process when a constructor parameter is a list and list is updated through an external function? There is "pass-by-reference" system when parameter is an array or list. But how is the object updated? Is any copy constructor used or how?

Let's assume that we have two user-defined classes Point and Curve. We filled our list with Point objects. Then we construct our Curve object with List of Points.

List<Point> points=new ArrayList<>();
points.add(Point(0,1));
points.add(Point(0,0));
Curve c=new Curve(points);

Then we add a Point object to our List of Points.

points.add(Point(1,1));

How is Curve object is affected?

BeginnerGuy
  • 49
  • 11
  • *`points.Add` typo `points.add` – rajuGT Nov 04 '15 at 15:14
  • The curve object now has 3 points in the List. Everything in java is passed by value, the reference of the curve object is not the same as `points`, but points(haha) to the same List. – user3719857 Nov 04 '15 at 15:17
  • @user3719857 we can't entirely know that's true without seeing the code for `Curve`. If the constructor is making a deep copy of the list, then adding to `points` will not affect it. – TayTay Nov 04 '15 at 15:20
  • 1
    @Tgsmith61591 actually that's the answer I was looking for. Thank you – BeginnerGuy Nov 04 '15 at 15:45

2 Answers2

0

c is essentially a pointer to the points object. Meaning the "value" of c internally contains the address of "points" object somewhere in the class.

From here on out, the changes you make in the points object will reflect over to the c object.

scerrecrow
  • 259
  • 1
  • 9
  • This is *wrong*! `c` is *not* a pointer to `points`, it's its own Object! `points` was merely passed as an argument to `Curve`, and we can't make the assertion that the reference to `points` remains inside `Curve` without seeing the source code for the `Curve` class (or viewing how `c` is affected after the `add` operation). If the constructor is making a deep copy of the list, then adding to points will not affect it. – TayTay Nov 04 '15 at 15:22
0

There is just a pass-reference-by-value but no pure pass-by-reference in java. If Curve stores the original value to the reference of points and points is not reinitialized then you are still working on the same reference and hence the List in c would also change(it´s still the same reference).

Here is a small example that should show you when you are working on the same reference and when not.

public class Curve{

    private List<Point> points = new ArrayList<>(0);

    public Curve(List<Point> points) {
        this.points = points;
    }

    public Curve(List<Point> points, boolean flag) {
        this.points.addAll(points);
    }

    void print() {
        for(Point p : points) {
            System.out.println(p);
        }
    }

    public static class Point {
        int x;
        int y;
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public String toString() {
            return "X = " + x +"\nY = " + y ;
        }

    }

    public static void main(String[] args) {
        List<Curve.Point> points = new ArrayList<Curve.Point>(0);
        points.add(new Curve.Point(0,0));
        points.add(new Curve.Point(0,1));
        // Care for compiler error just one should be used
        Curve c = new Curve(points,true); // Using this constructor copies the elements and the folloing add wont affect c
        Curve c = new Curve(points); // Using this constructor uses the same list so the following add will affect c
        points.add(new Curve.Point(1,1));
        c.print();
    }
}
SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
  • Thank you! It is more clear to me :) – BeginnerGuy Nov 04 '15 at 15:46
  • Kevin, it's worth noting that your `boolean flag` doesn't actually accomplish anything other than denoting a new constructor method signature. If you really wanted to, you could make a value of `true` copy the list and value of `false` merely assign the reference internally. – TayTay Nov 04 '15 at 16:14