File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about main()

 
geetu hindu
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
     
    Consider Paul's rocket mass heater.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic