Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7602175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:20:42+00:00 2026-05-30T23:20:42+00:00

Why are Hi1 and Hi3 displayed twice by the following code? static int a=1;

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:20:44+00:00Added an answer on May 30, 2026 at 11:20 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following piece of code was given to us from our instructor so we
so im practicing the STL string class, but i can not figure out why
The following code generates the primaey key for the new record to be inserted
If I want to explode a string by parts in PHP into an array,
static SerialPort port = new SerialPort(COM3, 57600, Parity.None, 8, StopBits.One); thread1() { lock(port) for(;;)
I'm trying to figure out how to lay out two of my Django models
I'm trying to work out why this function isn't working by placing a few
I want to split a string into two parts, the string is almost free
I have narrowed down my error to following set of codes producing different behaviour
I have the following XML: <query> <param weight=0.3> <item type=1 low=18 hi=20 pos=1 />

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.