"Cannot find character character: constructor ..." in Java?

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 it rsnpos.getString(1)contains a date in the format yyyy-MM-dd (e.g. 2010-01-01) ...

new df(rsnpos.getString(1))

During compilation, I get the following error ...

cannot find symbol
symbol  : constructor df(java.lang.String)
location: class df

I don’t understand why this is happening since I defined the constructor as shown in my code. Can someone please help me with this problem.

+3
source share
2 answers

This is not a constructor ... (constructors have an implicit "return type", a class type). It has an explicit return type and, therefore, is not a constructor, but a normal method with a name df.

, new df(...), . , new df().df("x") "" - String df(String).

, :

public class df 
{

  String dt;
  String datestring;
  // Remove return type (and keep matched name) to make it a constructor.
  public df(String dtstring) throws Exception
  {
    dt=dtstring;
    ...
    datestring = newformatter.format(outpdate);
    // Constructors cannot "return"
    // return datestring;
  }

}

, : -)

+1
public class df
     {
 String dt;
 String datestring;
 public 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);
        }
    }

. http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html.

+1

All Articles