Saturday, February 7, 2009

main method and Commandline args

Main() method:
public static void main(String arg[])
If you are keeping main method in any class at Run time we will get no such method error:main
class Sample { }

  • If we run the above program we will get No such method error:main
  • If you are changing to static to non static or void to some other return type main to any other name,we will get a RTE saying no such method error:main
  • Public and static we can inter change the order .
  • We can declare the main method a s final or synchronized.No compile time or run time error .But we can’t declare it as abstract.
  • In java ,the default value of Boolean is FALSE (in c and c++ it is true).
  • If we want we can over ride (method hiding) the main method.

Command line arguments:
class Sample {

public static void main(String arg[]) {
System.out.println(arg[0]); //100
System.out.println(arg[1]); //101
System.out.println(arg.length); //number of command args

>java Sample 100, 101
  • if we are accessing unspecified commandline arguments we will get a RTE saying array index out of bound exception.
  • In java commandline arguments are string form.if we want we can change.

Related Posts..........

var-arg method tutorial with example .


Post a Comment