Java Programming - Language Fundamentals - Discussion

Discussion Forum : Language Fundamentals - Finding the output (Q.No. 3)
3.
public class F0091 
{    
    public void main( String[] args ) 
    {  
        System.out.println( "Hello" + args[0] ); 
    } 
}

What will be the output of the program, if this code is executed with the command line:

> java F0091 world

Hello
Hello Foo91
Hello world
The code does not run.
Answer: Option
Explanation:

Option D is correct. A runtime error will occur owning to the main method of the code fragment not being declared static:

Exception in thread "main" java.lang.NoSuchMethodError: main

The Java Language Specification clearly states: "The main method must be declared public, static, and void. It must accept a single argument that is an array of strings."

Discussion:
18 comments Page 2 of 2.

Akash said:   1 decade ago
Can we use private instead of public in main method this question had been asked in my interview.

Aniruddh said:   1 decade ago
@Akash : Use of private with psvm() will lead to runtime exception : nosuchmainfound.

Mansha said:   10 years ago
While including static in public static void main it is still giving error that cannot find or load main class.

Balaji said:   10 years ago
If we add static on main method then also getting same result. Please guide me.

Artspade said:   8 years ago
using public static void main(String[] args) { s.o}
The result is world.

Riya said:   8 years ago
Why it is run time exception?

Teja said:   6 years ago
The output will be Hello World.

Sathish kumar said:   5 years ago
@Teja.

Kindly, explain your answer.


Post your comments here:

Your comments will be displayed after verification.