• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Are there different variations for the main method?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Members,

I have a doubt to ask you all.

In most online Java tutorials, the main method is given as follows: public static void main(String[] args)

However, in some examples, the main method is given as public static void main(String args[]) and also public static void main(String ... args)

I have tried running the different main methods and the programs work.

Does that mean that there are different variations of main method that are available?

Thank you.

 
Marshal
Posts: 65365
248
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Not something you need to know at the beginner's stage, but....

The square brackets operator [] and the ellipsis operator ... are both ways of converting the multiple command‑line arguments to an array of Strings. Two of the forms you showed are recommended in the Java┬« Language Specification (=JLS). The main method should be used to start the application, and you won't notice any difference between the two forms shown there. I would suggest that it is sometimes necessary to declare that parameter final, so you now have four forms.
  • public static void main(String[] args)...
  • public static void main(final String[] args)...
  • public static void main(String... args)...
  • public static void main(final String... args)...
  • I think there shouldn't be any space between String and ... but that is only my preference.
    Don't use the version you will find in old books with args[] in. It is regarded poor style to write the [] late, even though it will work. The [] is part of the type String[] and therefore should be written early.
     
    Saloon Keeper
    Posts: 10494
    224
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Not really. A method that takes an array as its last parameter counts as having the same method signature as a method that uses varargs instead of the array. The only difference is that you can call the variable arity method by providing the array elements as separate method arguments.

    If they really were different, you would be able to add both versions of the method to the same class. You can't.

    Personally I prefer the variable arity version. It makes sense, because you also call a program from the command line by providing the arguments separately from each other.
     
    Bartender
    Posts: 6115
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I prefer ellipses but Eclipse's formatter insists that there be no spaces on either side of the ellipses. Anyone know how to tell Eclipse not to do this?
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 10494
    224
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I prefer there to be no spaces between the type name and the ellipsis, but if you want to change it you can do so by going to Window -> Preferences -> Java -> Code Style -> Formatter. Select a profile you want to edit or create a new profile based on an existing one. Click Edit.... Navigate to White Space -> Declarations -> Methods, and check or uncheck the Before/After ellipsis in varargs parameters checkboxes depending on your preferences. Apply your changes.
     
    Rajan Masi
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you very much for your replies Members
     
    Campbell Ritchie
    Marshal
    Posts: 65365
    248
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Rajan Masi wrote:Thank you very much . . .

    That's a pleasure.
     
    Amateurs built google. Professionals built the titanic. We can't find the guy that built this tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!