Why are “Hi1” and “Hi3” displayed twice by the following code?
static int a=1;
public static void main(String[] args) {
if (a==2) { System.out.println(args[0]); a = 3;}
if (a==1) { main(); }
System.out.println("Hi1");
System.out.println(new PlayingWithMain().main("Hi3"));
}
public static void main() {
a = 2;
String[] a = new String[10];
a[0] = "Hi2";
main(a);
}
String main(String s) {
return s;
}
I have just started preparing for the OCPJP exam.
The first lesson — or trick, depending on how you look at it — of this question is that only one
mainmethod is special, no matter how manymainmethods are present. The special one is the one that takes the formIn the past, the argument had to be
String[] args, but for recent versions, var-args are also acceptable (e.g.String... args). JLS 12.1.4Now that we know which method to start with, we see that the first line checks the value of
a. We see that it’s initialized to 1, so we can ignore thea==2line. Then, on the next line, we jump to the no-argumentmain.In the no-arg
main,agets set to 2. The next lesson is that method-local variables can hide class variables. A newagets declared, and it takes precedence inside the method but only lives as long as the method does. It’s an array of strings of size ten, but only the first one is set (to “Hi2”). There’s one more lesson in this method: this code is written to make you think the string-argmaingets called next, but it doesn’t, because we haven’t created an object and it’s notstatic. Instead, we go back tomain(String[] args).This time,
ais 2 — remember, we set it in the no-argmain, andaisstaticso the change sticks around — so we print the first argument, “Hi2.” Next, we setato 3, so the upcominga==1test fails. In the following line, we print “Hi1” for the first time and create a new instance ofPlayingWithMain, which I assume is the class that the whole code snippet lives in.Since
aisstatic, its value remains 3 even for the new object. However, since the object is callingmain("Hi3"), we don’t go to astaticversion ofmain; instead, we go to the string-argmain. That method just kicks the input right back to the caller, where it gets immediately printed out.That does it for the string-array-arg
main, so we go back to the method that called it, the no-argmain. It’s also finished, so we go back again, to the version ofmain(String[] args)that the JVM called. Remember, we just completed the lineso we move on to printing “Hi1” again. Finally, we repeat the last line, which creates another new
PlayingWithMainobject and prints “Hi3” one last time.