+ 1

In d below program the Output will come as 2 3 1 100 how it vl come can anyone explain? how d sop Statement work here?

class A { static int i; static { i = 100; System.out.println(1); } static void staticMethod() { System.out.println(i); System.out.println(2); } } public class B { static { System.out.println(3); } public static void main(String[] args) { System.out.println(4); System.out.println(A.i); A.staticMethod(); } }

2nd Sep 2016, 2:56 PM
suji prem
suji prem - avatar
7 Answers
+ 2
But in my computer the answer is 3 4 1 100 100 2
2nd Sep 2016, 3:14 PM
Azhy Slemany
Azhy Slemany - avatar
+ 1
dude d question is r8 it was given as assignment in class
2nd Sep 2016, 3:35 PM
suji prem
suji prem - avatar
+ 1
i checked it bro it gave d output as i hv mentioned in question!!
2nd Sep 2016, 4:02 PM
suji prem
suji prem - avatar
+ 1
sorry man no idea how it's working
2nd Sep 2016, 4:02 PM
Abhijay Bisht
Abhijay Bisht - avatar
+ 1
when you run the program it looks main method.so firstly creating B class and loading static content and printing 3,then program running main method ,in first println it prints 4 and second println creating A class firstly and loading static content in bracles and turning back println method and gets A.i value and printing it. After these program call A.staticMethod .
2nd Sep 2016, 8:41 PM
bayram akdemir
bayram akdemir - avatar
0
Dude question is wrong or not fully written. Pls write full ques or explain understandable.
2nd Sep 2016, 3:34 PM
Abhijay Bisht
Abhijay Bisht - avatar
0
why don't u run it and check to find out
2nd Sep 2016, 3:37 PM
Abhijay Bisht
Abhijay Bisht - avatar