-4

I've written a small program of a car advertisement, but I'm confused as to why my variable c1 does not update to the newest car object that was made in my setter methods.

public class coolCars {
    public static void main(String args[]) {
        Car c1 = new Car("Barry", "998D", "Prius", 50, 1800.50);
        System.out.print(c1);
        c1.setOwner("Jerry");
        System.out.println(c1.owner());
        /*c1.setOwner("Mick");
        System.out.print(c1);
        c1.setKil(70);
        System.out.print(c1);*/
    }
}

And then here's my Car class

final class Car {
    private final String owner;
    private final String reg;
    private final String make;
    private final int kilometres;
    private final double price;
    public Car(String ow, String r, String m, int k, double p) {
        owner = ow; reg = r; make = m; kilometres = k; price = p; 
    }
    public String owner(){return this.owner;}
    public String reg(){return this.reg;}
    public String make(){return this.make;}
    public int kilometres(){return this.kilometres;}
    public double price(){return this.price;}
    public Car setPrice(double p){return new Car(owner(), reg(), make(), kilometres(), p);}
    public Car setOwner(String ow){return new Car(ow, reg(), make(), kilometres(), price());}
    public Car setKil(int k){return new Car(owner(), reg(), make(), k, price());}
    public Car setMake(String m){return new Car(owner(), reg(), m, kilometres(), price());}

    public String toString(){return "(" + owner + " " + reg + " " + make + " " + kilometres + " " + price + ") ";}
}

As mentioned above, the owner variable is not updating which means c1 is still pointing at the initial instance of the object. Any ideas? Thanks.

Water
  • 3,245
  • 3
  • 28
  • 58
petegoast
  • 55
  • 8
  • 1
    Please provide a [mcve] and format it a *lot* more readable than your current example. But fundamentally, your `setOwner` method creates a *new* `Car` instance and returns a reference to it, which you're then ignoring in your calling code. If you had `c1 = c1.setOwner("Jerry");` it would work. Why would you expect just creating a new instance of the object to change the value of your `c1` variable? – Jon Skeet Nov 12 '17 at 16:45
  • Thanks that really worked. You're definitely right, now that I'm looking at it. I'm just learning about importance of immutables and I guess I expect c1 to 'automatically' change it's car to the new car. Thanks! – petegoast Nov 12 '17 at 16:52
  • I would rename the `setOwner` method to `withOwner`, personally (and the rest of the methods). That would be more intuitive in terms of what the methods do. – Jon Skeet Nov 12 '17 at 16:52
  • Definitely related: https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Kayaman Nov 12 '17 at 17:10

3 Answers3

1

You are not setting values in all these methods:

public Car setPrice(double p){return new Car(owner(), reg(), make(), kilometres(), p);}
public Car setOwner(String ow){return new Car(ow, reg(), make(), kilometres(), price());}
public Car setKil(int k){return new Car(owner(), reg(), make(), k, price());}
public Car setMake(String m){return new Car(owner(), reg(), m, kilometres(), price());}

You are just returning new Instance of Car that does nothing.

Do like this:

public Car setPrice(double price){ this.price = price }
public Car setOwner(String owner){ this.owner = owner }
public Car setKil(int kilometres){ this.kilometres = kilometres}
public Car setMake(String make){ this.make = make }
icarumbas
  • 1,777
  • 3
  • 17
  • 30
0

The setOwner method is creating a new instance of Car. In your main method C1 is still pointing to the originally created car object. You would need to c1 = c1.setOwner. However this seems counterintuitive. Your setter method should simply update the class level variable of Car for that object. Not create a brand new object.

JoeN
  • 51
  • 1
  • 7
0

Each of the Setter methods (for price, owner, kilometres, and make) are creating a new Car object and returning it.

In the following statement, there is no assignment:

c1.setOwner("Jerry");

Therefore, the returned Car object (which was newly created) is getting lost.

modify by assigning it and then try:

c1 = c1.setOwner("Jerry");

Or can correct the Setter methods themselves as suggested above.

Saurabh
  • 23
  • 4