When you declare any local/block variable, they didn’t get the default values. They must assigned some value before accessing it other wise compiler will throw an error. If the variable has a global scope then default value can be assigned and accessed. if the variable is of reference type then the default value will be null
. this link contains default values for the primitive datatype:
The compiler will not permit this(since it is local/block variable):
public static void samplemethod()
{
int a;
int b = a;
}
where as the following code works fine since the variable have global scope:
public int i;
public void samplemethod()
{
int a;
int b = i;
}