Reputation: 113
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.
Upvotes: 0
Views: 133
Reputation: 30828
The first lesson — or trick, depending on how you look at it — of this question is that only one main
method is special, no matter how many main
methods are present. The special one is the one that takes the form
public static void main( /* multiple arguments */ ) { ... }
In 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.4
Now 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 the a==2
line. Then, on the next line, we jump to the no-argument main
.
In the no-arg main
, a
gets set to 2. The next lesson is that method-local variables can hide class variables. A new a
gets 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-arg main
gets called next, but it doesn't, because we haven't created an object and it's not static
. Instead, we go back to main(String[] args)
.
This time, a
is 2 — remember, we set it in the no-arg main
, and a
is static
so the change sticks around — so we print the first argument, "Hi2." Next, we set a
to 3, so the upcoming a==1
test fails. In the following line, we print "Hi1" for the first time and create a new instance of PlayingWithMain
, which I assume is the class that the whole code snippet lives in.
Since a
is static
, its value remains 3 even for the new object. However, since the object is calling main("Hi3")
, we don't go to a static
version of main
; instead, we go to the string-arg main
. 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-arg main
. It's also finished, so we go back again, to the version of main(String[] args)
that the JVM called. Remember, we just completed the line
if (a==1) { main(); }
so we move on to printing "Hi1" again. Finally, we repeat the last line, which creates another new PlayingWithMain
object and prints "Hi3" one last time.
Upvotes: 2
Reputation: 2478
main(String[]) calls main() which again calls main(String[]) if a==1, which is true at the beginning.
The a variable is used to make this recursion only happen once and not endlessly.
This is why the main(String[]) method is executed twice, and this is why the output written from that method appears twice.
Upvotes: 0