Question

Why is my constructor not applying the variables?:

import java.awt.geom.Point2D;

public class Waypoint extends Point2D.Double{

private double s;
private String street;

public Waypoint(double x, double y, double s, String street) {
    super(x,y);
    s=9;
    street="Street";
}
   }

What is missing here?

Was it helpful?

Solution

Change

import java.awt.geom.Point2D;

public class Waypoint extends Point2D.Double{

    private double s;
    private String street;

    public Waypoint(double x, double y, double s, String street) {
        super(x,y);
        s=9;
        street="Street";
    }

To

import java.awt.geom.Point2D;

public class Waypoint extends Point2D.Double{

    private double s;
    private String street;

    public Waypoint(double x, double y, double s, String street) {
        super(x,y);
        this.s = s;
        this.street = street;
    }

You are not using the values passed to the constructor.

OTHER TIPS

These lines...

s=9;
street="Street";

...assign values to the local variables corresponding to the arguments of your constructor, while you want to assign them to the fields instead, like this:

this.s = s;
this.street = street; // or "Street", if you prefer

The this. prefix makes the compiler understand that you are referring to the field, and not to the local variable of the same name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top