What is the output of the following?.
public class Main { {/* www . jav a 2s . co m*/ System.out.print("1"); } static { System.out.print("2"); } public Main() { System.out.print("3"); } public static void callMe() { System.out.print("4"); } public static void main(String[] args) { callMe(); callMe(); System.out.print("5"); } }
B.
This class is never instantiated, so the instance initializer never outputs 1 and the constructor never outputs 3.
This rules out Options A, D, and E.
A static initializer only runs once for the class, which rules out Option C.
Option B is correct because the static initializer runs once printing 2, followed by the static method callMe()
printing 4 twice, and ending with the main()
method printing 5.