Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35597

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.

Upvotes: 0

Views: 2638

Answers (7)

Maqsood Wani
Maqsood Wani

Reputation: 11

  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.

Upvotes: 1

commit
commit

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

radai
radai

Reputation: 24212

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.

Upvotes: 1

Philipp Sander
Philipp Sander

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

Ankur Lathi
Ankur Lathi

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

Prasad Kharkar
Prasad Kharkar

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

ZeusNet
ZeusNet

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

Related Questions