0

Why the third static block is getting printed? Why not it’s printing the first or the second static block

class JavaExample2{ static int num; static String mystr; //First Static block static{ System.out.println("Static Block 1"); num = 68; mystr = "Block1"; } //Second static block static{ System.out.println("Static Block 2"); num = 98; mystr = "Block2"; } //Third static block static{ System.out.println("Static Block 3"); num = 108; mystr = "Block3"; } public static void main(String args[]) { System.out.println("Value of num: "+num); System.out.println("Value of mystr: "+mystr); } }

14th Jan 2019, 5:47 PM
Pramod Nayak
Pramod Nayak - avatar
2 Antworten
+ 3
hi. you declared both variables (num and mystr) in the scope of javaexample2. in that class they receive 3 values. num is first 68, then 98 and at the end 108. same happens with mystr variable and at the end it stores "Block3". in your main you printed the value of those variables and it printed the values found stored in them at the end. hope i helped a bit
14th Jan 2019, 8:32 PM
notqueued
notqueued - avatar
+ 1
Heya, thanks for the answer!
14th Jan 2019, 9:28 PM
Pramod Nayak
Pramod Nayak - avatar