[Solved]Why output of this two code is different? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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)

25th Aug 2019, 11:50 AM
Amir01
Amir01 - avatar
1 Answer
+ 1
because method variables do not have default values and class variables have default values
25th Aug 2019, 4:26 PM
zemiak