Reputation: 646
i have a class with an construktor with two arguments of type double.
public class NetcdfHandler {
double x;
double y;
public NetcdfHandler (double x, double y){
x = this.x;
y = this.y;
}
}
When i call this in the doGet Method of my Servlet, the given double values somehow are not "received" by the constructor. That means after creating an instance of NetcdfHandler, x and y both have the value 0.0 (but not "null") although "Lat" and "Lng" are set up correctly:
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;
double Lat;
double Lng;
public Test() {
super();
}
protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
Lat = Double.parseDouble(req.getParameter("lat"));
Lng = Double.parseDouble(req.getParameter("lng"));
NetcdfHandler nc = new NetcdfHandler(Lat, Lng);
}
I guess it's quite a beginners mistake, but i couldn't figure out whats the problem here. Maybe someone can help?
Upvotes: 0
Views: 140
Reputation: 100199
Your constructor is incorrect. Write this way:
public NetcdfHandler (double x, double y){
this.x = x;
this.y = y;
}
Currently you're doing the opposite thing: put the default field values (which are 0.0) into parameters. So fields are not changed and parameters are forgotten upon constructor exit.
If you are not going to change fields x
and y
after construction, it's better to declare them final:
final double x;
final double y;
This way it's easier to prevent some programming mistakes. In particular your code would result in compilation error like "final field is not initialized in constructor".
Upvotes: 4