+ 1
[Solved]Why output of this two code is different?
//code 1 class Program{ static String str; public static void main(String[] args){ System.out.println(str+"hello"); } } //Output nullhello //code 2 class Program{ public static void main(String[] args){ String str; System.out.println(str+"hello"); } } //Output Error(variable str might not have been initialized)
2 Answers
+ 1
because
method variables do not have default values and
class variables have default values