Java Programming - Language Fundamentals - Discussion

Discussion :: 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

[A]. Hello
[B]. Hello Foo91
[C]. Hello world
[D]. The code does not run.

Answer: Option D

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."


Shiwam said: (Sep 6, 2011)  
If it is declared as static then o/p is ?

Purnima said: (Nov 6, 2011)  
Here I mentioned as public static void main.

Still I m getting run time exception. What will be the prob? can anyone explain? Please.

Alex said: (Nov 7, 2011)  
Even if you put public static void main you will get Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
because of exceeding array limit by o/p

Rochu said: (Mar 6, 2012)  
If main method contain static keyword then what will be the result. Please help me.

Rupa said: (Apr 13, 2012)  
It is declared as static then o/p is Helloworld.

Jyothirmai said: (May 28, 2012)  
Why should we use static in before main method?

Nikhila said: (Jun 27, 2012)  
@Jyothirmai.

Main method cannot be a instance method so we need need to use static keyword. For clear idea consider instance method or instance variable so to call them we need create an object in main method. If main method itself is in instance method so where we need to create an object for that instance main method.

I think you got my point.

Sudheer said: (Jul 3, 2012)  
Static methods executes first than the constructor or instance method. Static method will execute in heap when the object of the class is created.

If we do not make the main method as static who will call the main method in our class, that's why when the class object is created the static methods are automatically executed by JVM.

Ganesh said: (Dec 6, 2012)  
If main method contain static keyword. What is the result for that code please explain?

Sambhunath Samanta said: (Apr 23, 2013)  
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.

Akash said: (Jul 19, 2013)  
Can we use private instead of public in main method this question had been asked in my interview.

Aniruddh said: (Aug 10, 2013)  
@Akash : Use of private with psvm() will lead to runtime exception : nosuchmainfound.

Mansha said: (Jul 2, 2015)  
While including static in public static void main it is still giving error that cannot find or load main class.

Balaji said: (Jul 13, 2015)  
If we add static on main method then also getting same result. Please guide me.

Artspade said: (Jan 12, 2017)  
using public static void main(String[] args) { s.o}
The result is world.

Riya said: (Oct 6, 2017)  
Why it is run time exception?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.