Sunday, September 11, 2011

2. Main() and command line argument

- The compiler will check for the main method declaration during the run-time of the program and throws the Run-time Exception

Example:

class test
{

// Some code without main method

}
> javac test.java //done
> java test // Run-time Exception NoSuchMethod found Error

Syntax of Main() method

public static void main(String[] args)

- public: to call this method from anywhere by JVM.
- static: this will make the main method to be called without creating instance of the main().
- void: method won't return anything to the JVM.
- main: it is a thread which the JVM looks first and is the entry point for the program. This method only configured in JVM.
- String[]:  that is java will accept every thing as a string array.
- args: This defines a parameter to the main method and will accept command line options passed by the user while invoking the progam.

simple example:
class best
{
public static void [b]main(String[] args)
{
System.out.println("good");
}
}

- if we are performing any change to the above syntax, we will get run-time exception saying no such method Exception

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.