The code is as simple as it can possibly get, but I seem to be getting a compiler error. What did I miss?
As a side-note, removing the _name
field altogether simply generates the same error on the next field.
P.S.: Expecting quite a few minus votes on this one, it feels like I'm missing something really-really simple.
package mkumpan.helpers;
public final class BotState
{
private final String _name;
private final double _x;
private final double _y;
private final double _energy;
private final double _heading;
private final double _velocity;
public BotState(
String name,
double x,
double y,
double energy,
double heading,
double velocity
) {
String _name = name;
double _x = x;
double _y = y;
double _energy = energy;
double _heading = heading;
double _velocity = velocity;
} // BotState.java:26: error: variable _name might not have been initialized
public String getName() { return _name; }
public double getX() { return _x; }
public double getY() { return _y; }
public double getEnergy() { return _energy; }
public double getHeading() { return _heading; }
public double getVelocity() { return _velocity; }
}
You must initialize the final fields, but you just initialized local variables in the constructor.
Change
String _name = name;
double _x = x;
double _y = y;
double _energy = energy;
double _heading = heading;
double _velocity = velocity;
to
this._name = name;
this._x = x;
this._y = y;
this._energy = energy;
this._heading = heading;
this._velocity = velocity;