Home Java: Reference variable of object not updating to new instance of object?
Reply: 3

Java: Reference variable of object not updating to new instance of object?

petegoast
1#
petegoast Published in 2017-11-12 16:43:38Z

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.

icarumbas
2#
icarumbas Reply to 2017-11-12 16:49:50Z

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 }
JoeN
3#
JoeN Reply to 2017-11-12 16:52:42Z

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.

Saurabh
4#
Saurabh Reply to 2017-11-12 17:00:50Z

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.312497 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO