Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Main Methods

 
Josh Forrest
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can i pass in parameters to the main class?
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic