-2
import java.util.Date;
import java.text.SimpleDateFormat;
class Test
{
  public static void main(String args[])
  {
   Date d;
   SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy");
    try{
    d=sdf.parse("20-12-2016",0);
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
    System.out.println(d);
   }
}

When I compile this code I get

error: variable d might not have been initialized in

System.out.println(d);

but I have assigned it a date using parse function in

d=sdf.parse("20-12-2016",0);
nicky97
  • 35
  • 8

1 Answers1

-1

These here:

Date d;
SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy");
try{
    d=sdf.parse("20-12-2016",0);
}
   catch(Exception ex){
   ex.printStackTrace();
}
    System.out.println(d);
}

is a little bit dangerous, because if your try catch doesn't work, then you are passing a non initialized object as parameter (printing a date object that is not initialized)

the IDE is smart enough to prevent such thing, that is the reason it is complaining..

initialize the value when defining it.. and everything will be ok.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97