0

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; }
}
Maxim Kumpan
  • 2,545
  • 2
  • 19
  • 23

2 Answers2

6

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;
René Link
  • 48,224
  • 13
  • 108
  • 140
  • 1
    Whoops... inattentiveness will be my ruin someday. Too much type copy-pasting. Kudos! P.S.: Can and will accept in 10 minutes. – Maxim Kumpan Sep 16 '13 at 09:16
0

No need to put variable type like "double" again inside the constructor body between {}.

Bimalesh Jha
  • 1,464
  • 9
  • 17