Reputation: 35597
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
.
Upvotes: 0
Views: 2638
Reputation: 11
Upvotes: 1
Reputation: 4807
One point as all explain there is no way to pass object from console so it's meaningless.
Still as I also think Object is super class so why jvm does not understand it, but there is also other point that if jvm allowed to accept Object arguments than user can pass non-string variable as well so there jvm will create error that's why I think jvm make restrict to pass string variable.
Upvotes: 1
Reputation: 24212
there are a few ways to answer this
Upvotes: 1
Reputation: 10249
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.
Upvotes: 2
Reputation: 7836
The String[]
were for command-line arguments, strings are what the user enters at the command line. Objects can't be entered from Command line.
From JLS:
The method main must be declared public, static, and void. It must specify a formal parameter whose declared type is array of String. Therefore, either of the following declarations is acceptable:
public static void main(String[] args)
public static void main(String... args)
Upvotes: 1
Reputation: 13576
The arguments passed to the main method are from command line. So they are String
main method can also be written like this
public static void main(String... args) {
}
Upvotes: -1
Reputation: 720
The main Method of Java is specified with strings as parameters. So the compiler can't detect the main-method with objects as args. I guess the resaon for this is, that the main method are usally called due an console command, and there you have only the opportunity to set strings as parameters.
Upvotes: 2