0
When did Java start the default value or variable initialization?
For example, if I declare a vaiable boolean flag; (not initialized), then at next lines below if I write System'.out.println(flag); Java will give error message: The variable flag might have not been initialized. I forgor. Have you ever told me that default value of boolean variabble if not initialized when declared is false?
2 Answers
+ 4
Local variable are not initialized by default at opposite as static and class member variables
+ 4
//See here no values set, getting defaults:
class defaultDemo {
int x;
double y;
String s;
boolean b;
}
class xyz {
public static void main(String[] a){
defaultDemo obj = new defaultDemo();
System.out.print(obj.x+"\n"+
obj.y+"\n"+
obj.s+"\n"+
obj.b);
}
}
output:
0
0.0
null
false
//edit: static boolean b ; also will have false