File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Java Main Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Main Methods" Watch "Java Main Methods" New topic

Java Main Methods

Josh Forrest

Joined: Oct 08, 2002
Posts: 7
can i pass in parameters to the main class?
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
for what I know, method main has the following assignature:
public static void main(String args[]);
(correct me if I'm wrong)
if somebody has the answer for why the signature can't be:
public static void main(Object obj);
tell me!!!
Greg Charles

Joined: Oct 01, 2001
Posts: 2968

public static void main(Object obj)
is a perfectly valid method signature. It just won't be run as the result of a command line execute. When you run "java MyClass", the JVM looks for a method in MyClass with the signature:
public static void main(String [] args)
If it doesn't find one, then it will exit with an error message. They could have created the JVM to look for a method of a different signature, but they didn't.
Anyway, to the original question ... if you run:
java MyClass a b c
then a, b, and c will be available in your "main" method as args[0], args[1], and args[2].
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
For those not already familiar with Sun's Java Turorial, it does a decent job of covering many basic Java topics including The Command-Line Arguments Section and The main Method Section.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: Java Main Methods
It's not a secret anymore!