I have a class defined as follows…
public class df {
String dt;
String datestring;
public String df(String dtstring) throws Exception {
dt=dtstring;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date inpdate = formatter.parse(dt);
datestring = formatter.format(inpdate);
Date outpdate = formatter.parse(datestring);
SimpleDateFormat newformatter = new SimpleDateFormat("dd/MM/yyyy");
datestring = newformatter.format(outpdate);
return datestring;
}
}
I create instances of this class as follows, where rsnpos.getString(1) contains a date in yyyy-MM-dd format (e.g. 2010-01-01)…
new df(rsnpos.getString(1))
During compilation, I am getting the following error…
cannot find symbol
symbol : constructor df(java.lang.String)
location: class df
I don’t understand why this is happening, as I have defined a constructor as shown in my code. Could someone please assist me with this problem.
That’s not a constructor… (constructors have an implicit “return type”, the type of the class). That has an explicit return type, and is thus not a constructor, but a normal method named
df.Thus it is invalid when used as
new df(...), which is exactly what the error message says. On the other hand,new df().df("x")would still “work” because of the default parameterless constructor and the methodString df(String).Note the updates to change it into a constructor:
Please work on variable names and naming conventions and mutability redux 🙂