samedi 29 août 2015

Why can't we define variable just afte if statement Like follows

public class Foo{

public static void main(String []args){

int a=10;

if(a==10)

int x=20;

}
}

While compiling above code, an error is thrown at compile.

But, in the below code it is fine. Why?

public class Foo{

public static void main(String []args){

int a=10;

if(a==10){

int x=20;
}
}
}

I don't get it. What exactly is happening in these? As I understand, we can write one statement after an if statement without curly braces (compound statement block).

Aucun commentaire:

Enregistrer un commentaire