Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first lesson &#8212; or trick, depending on how you look at it &#8212; of this question is that only one <code>main</code> method is special, no matter how many <code>main</code> methods are present. The special one is the one that takes the form</p> <pre><code>public static void main( /* multiple arguments */ ) { ... } </code></pre> <p>In the past, the argument had to be <code>String[] args</code>, but for recent versions, var-args are also acceptable (e.g. <code>String... args</code>). <a href="http://docs.oracle.com/javase/specs/jls/se5.0/html/execution.html#12.1.4" rel="nofollow">JLS 12.1.4</a></p> <p>Now that we know which method to start with, we see that the first line checks the value of <code>a</code>. We see that it's initialized to 1, so we can ignore the <code>a==2</code> line. Then, on the next line, we jump to the no-argument <code>main</code>.</p> <p>In the no-arg <code>main</code>, <code>a</code> gets set to 2. The next lesson is that method-local variables can hide class variables. A new <code>a</code> 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 <code>main</code> gets called next, but it doesn't, because we haven't created an object and it's not <code>static</code>. Instead, we go back to <code>main(String[] args)</code>.</p> <p>This time, <code>a</code> is 2 &#8212; remember, we set it in the no-arg <code>main</code>, and <code>a</code> is <code>static</code> so the change sticks around &#8212; so we print the first argument, "Hi2." Next, we set <code>a</code> to 3, so the upcoming <code>a==1</code> test fails. In the following line, we print "Hi1" for the first time and create a new instance of <code>PlayingWithMain</code>, which I assume is the class that the whole code snippet lives in.</p> <p>Since <code>a</code> is <code>static</code>, its value remains 3 even for the new object. However, since the object is calling <code>main("Hi3")</code>, we don't go to a <code>static</code> version of <code>main</code>; instead, we go to the string-arg <code>main</code>. That method just kicks the input right back to the caller, where it gets immediately printed out.</p> <p>That does it for the string-array-arg <code>main</code>, so we go back to the method that called it, the no-arg <code>main</code>. It's also finished, so we go back again, to the version of <code>main(String[] args)</code> that the JVM called. Remember, we just completed the line</p> <pre><code>if (a==1) { main(); } </code></pre> <p>so we move on to printing "Hi1" again. Finally, we repeat the last line, which creates another new <code>PlayingWithMain</code> object and prints "Hi3" one last time.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload