0
What is the output of below code
public class StaticBasics{ private static int count; static{ System.out.println("static block"); System.out.println(count); main(null); } public static void main(String[] arhs) { System.out.println("main method"); } }
4 Réponses
+ 3
You could have just run the code in the code playground
BTW, class keyword is missing and identifier is missing for the count variable
+ 1
Just like Sami Khan mentioned, this would give you an output.
public class StaticBasics{
private static int count;
static{
System.out.println("static block");
System.out.println(count);
main(null);
}
public static void main(String[] arhs) {
System.out.println("main method");
}
}
Static blocks are executed at the time of class loading so they are run even before the main method is executed. You can run the code to see the output for yourself.
0
No
0
Read the code carefully