0

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.

8
  • 6
    Apart from the syntax aspect, how do you expect to pass a Java Object to a Java program from the command line? Commented Aug 13, 2013 at 9:26
  • You would have to parse strings all the time... Commented Aug 13, 2013 at 9:27
  • 2
    That's just the way it's specified - see section 12.1.4 of the JLS... Commented Aug 13, 2013 at 9:28
  • @assylias Object array can accept any object type String, Integer. so it doesn't matter Commented Aug 13, 2013 at 9:28
  • 5
    I wonder why the question have negative votes. I think this is good question. Plus, this question is answerable though. Commented Aug 13, 2013 at 9:29

7 Answers 7

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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)

Comments

1

there are a few ways to answer this

  1. "because". the main() entry point to a program is specified like this and canot be overloaded to accept other arguments
  2. as hinted by assylias, the main() method is expected to be invoked from a comamnd line or shell. this means that the arguements will always be strings. whatever you type on a command line is a string.

Comments

1

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.

Comments

1
  1. JVM calls main() as a thread. When we call main() from another method in java, we call it as function or method.
  2. Actually the confusion here is " why auto casting is not applicable when we try to call main(Object args) from console." May be this restriction has been put in native methods of JVM to avoid the complications.
  3. I you guys observe same case u will find for catch(Object e) Summery line: it is the native methods code of JVM which restricts the auto casting, to avoid complications.

1 Comment

I think it is a valid point that to avoid complications JVM does not upcast String[] to Object[].
-1

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) {

 }

2 Comments

I downvoted. The rephrased OP's question is : Why java does not give opportunity to write public static void main(Object[] args). The content of your answer is correct and good to know but I considered it as off-topic. Your advice can be put in the comments.
Thanks for pointing that out. Still, you can read the first line of my answer and realize that answers OPs question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.