File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes question about main() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question about main()" Watch "question about main()" New topic
Author

question about main()

geetu hindu
Greenhorn

Joined: Apr 12, 2006
Posts: 9
why do we use static keyword in main()?

second question

can i define any type of parameter in main like :
public static void main(int args[])

and can we define more than one parameter
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

"static" in front of a method name means that you can call the method without actually creating the object. if you didn't have static there, main couldn't run unless you created an object, but you can't create an object until you start your program.

now, you CAN create a method like you list, but it won't be the one that the JVM tries to run when you start your program. the JVM looks for the method with the specific signiature

public static void main (String [] args)

so if you have only one with an int array, the JVM won't be able to run your program.

at least, that's how i understand it.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
You are right that it will compile but not run, because the JVM searches for the signature.
It is possible to kid BlueJ into taking a main method with a different signature, but then BlueJ isn't a JVM. I have managed to get myJVM to accept these signatures:-but I don't think ti's a good idea for real-life use.
CR
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Syntactically, the order of modifiers (public static versus static public) isn't significant, but as a matter of style, almost all Java programmers put the access (public) first.


There is no emoticon for what I am feeling!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I think the "public static void main(String ...)" signature is available for any JVM that supports Java 1.5. This is the syntax for variable argument lists (varargs) that gets translated into an array anyway. There are several other variations that are allowed, but most of them are just syntactic variations on the one you are used to, like switching the order of "public" and "static".

Layne
[ April 25, 2006: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
We didn't notice that Geetu Hindu asked whether you can define more than one parameter.

What you do is pass an array of Strings, which can have any number of members. So yes, you can pass several parameters.

/>c: java MyClass Campbell "Campbell Ritchie" 23

has 3 parameters:
  • Campbell
  • Campbell Ritchie
  • 23

  • You will probably however, have to write out a Readme.txt file to accompany your class, with a description in of what you need to pass. Otherwise you will forget.

    CR
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: question about main()