string - Java error while trying to print a variable initialized within if -


i'm trying print string initialized within if-else statement. i'm having error regarding datestr may not have been initialized in last line. suggestion? code:

   int currentday = localdatetime.now().getdayofmonth();    int currentmonth = localdatetime.now().getmonthvalue();    int currentyear = localdatetime.now().getyear();    string datestr;    if (currentday < 10 && currentmonth < 10){                datestr = "0" + currentday + "/0" + currentmonth + "/" + currentyear;     } else if (currentday < 10 && currentmonth >= 10) {          datestr = "0" + currentday + "/" + currentmonth + "/" + currentyear;     } else if (currentday >= 10 && currentmonth >= 10){          datestr =  currentday + "/" + currentmonth + "/" + currentyear;     }        system.out.println(datestr); 

you missing else case without string may not have been initialized.

otherwise initialize default value, such null or empty string:

string datestr = ""; 

Comments