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 1 of 2.

Sambhunath Samanta said:   1 decade ago
When we access anything from any method then first we create object of this method after that we access but if we declare method is static then we can access anything from this method without create object.

That why we use before main static keyword.
(1)

Sathish kumar said:   4 years ago
@Teja.

Kindly, explain your answer.

Shiwam said:   1 decade ago
If it is declared as static then o/p is ?

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

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

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

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

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

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

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


Post your comments here:

Your comments will be displayed after verification.