Wednesday, 14 August 2013

Why instance varibales have default values in java?

Why instance varibales have default values in java?

why the variables declared in class have default values but the variables
declared inside methods or said to be local variables dont have default
values in java? for example
class abc
{
int a;
public static void main(String ss[])
{
int b;
abc aa=new abc();
System.out.println(aa.a);
System.out.println(b);
}
}
In this above example variable a has default value of 1 but variable b
give error that it might not have been initialized.

No comments:

Post a Comment