• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question about main()

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
     
    reply
      Bookmark Topic Watch Topic
    • New Topic