Search code examples
javaprogram-entry-point

Main method in java only accept String[]


Object is the super type of all classes in Java. Consider my following class

public class Test {
public static void main1(Object[] args) {
    System.out.println("I accept an object array");
}
public static void main(String[] args) {
   main1(args);
}
}

Due to object superiority object array can accept any object type arrays. But Still java doesn't consider following class contains a main method.

public class Test {   
public static void main(Object[] args) {

 }
} 

Why java never give this opportunity while object is ultimate supper type for all classes in java.


Solution

  • because java looks explicitly for public static void main(String[] args) when running. specified in 12.1.4 of the jls

    The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String. Therefore, either of the following declarations is acceptable:

    Object wouldn't make sense, because you can not pass an other object through the console.