0

my code is:-

class Test
{
  static int a = 11;
  static
  {
    System.out.println("Hello static! " + main() + a);
  }

  public static void main(String[]args)
  {
    System.out.println("Hello String!");
  }

  public static char main()
  {
    System.out.println("Hello char!");
    return 'H';
  }
}

Output:-

Hello char!
Hello static! H11
Hello String!

Why "Hello char!" is printed before "hello static!"?

5 Answers 5

3
  1. The static block executes first.
  2. It calls main() with no arguments while assembling a String to print.
  3. main() prints some output.
  4. The static block prints its output.
  5. The static block exits.
  6. Your main(String[]) method executes and prints its output.
Sign up to request clarification or add additional context in comments.

2 Comments

can u please explain me the working of static block to make it more clear why the main() is called first before the first argument of println(), i.e., "Hello static!" ?
@HimanshuAggarwal I've already done that. See #2: 'while assembling a String to print'. The argument to printn() has to be evaluated before println() can be called. Part of that evaluation consists of calling main().
1

Why "Hello char!" is printed before "hello static!"?

Because, main() method is invoked and executed before the completion of the sysout statement printing Hello static.

Here's the execution order: -

  • Class is loaded.
  • static variable a is loaded and initialized to 11.
  • static block is executed.
  • From sysout statement there, main() static method is invoked. At this point of time Hello static has not been printed yet, because the sysout statement is not complete.
  • Execution control goes to main() method. Because without that the current sysout statement cannot complete.
  • main() method prints "Hello char", and returns 'H'.
  • Execution goes back to static block, inside sysout.
  • sysout completes execution, and prints "Hello static! H11"

Note: - sysout above means - System.out.println().

3 Comments

There is no such thing as a 'sysout statement'. Do you mean the System.out.println method invocation?
it was clear from the text. no problem. thanx for the answer.
@RohitJain You shouldn't talk about a statement when you mean a method call. It's clear enough what you actually meant, but that doesn't excuse the misuse of standard terminology.
1

Let's break it down.

The very first thing that runs is the static initializer block. So we run this line:

System.out.println("Hello static! " + main() + a);

But before we can call that method, we have to call main(), because we have to fully evaluate all arguments first.

So this gets called before that println in the static initializer:

public static char main()
{
  System.out.println("Hello char!");
  return 'H';
}

And that prints the Hello char! we see in the first line.

It returns H, which gets printed alongside Hello static!

Finally, main(string[]) is called to begin the program. And there we see Hello String!

Comments

1

The general execution logic here is as follows:

  1. The class is loaded, hence the static constructor is executed.
  2. Inside the constructor, the String (parameter to System.out.println()) is evaluated, which calls the char main() method.
  3. The char main() method is evaluated.
  4. Inside the char main() the string is printed: 1st output line
  5. The String in the static constructor has been build and thus can be printed: 2nd output line
  6. The actual void main() can be executed, which prints the third line: 3rd output line

Comments

1

In your line

System.out.println("Hello static! " + main() + a);

main() is executed before the System.out.println, so it makes sense that the print statement within your char main() would execute before the outer print statement "Hello Static".

3 Comments

but why is the main() called first? that is what my doubt is.
it's like if you have the statement a = a + func(); func() needs to fully evaluate before you can add and assign to a.
ohh! is this the case? then its fine. thanx.:-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.