vendredi 2 décembre 2016

Single line If statement giving compilation errors while trying to declare a variable inside the If block. What is the reason?

A single line If statement is giving compilation errors while trying to declare and assign a variable inside the single lined If block. Here's the code:

if(true)
    String msg = "Hellow";

enter image description here

But once I declare the variable outside the If block, it doesn't complain any more. Here's the code:

String msg;
if(true)
    msg = "Hellow";

Can someone please explain me why?

Aucun commentaire:

Enregistrer un commentaire