Is void main() OK in this case?

Discussion in 'Java' started by Chuck Heathfield, Feb 3, 2009.

  1. Hello,

    Basic question.

    public static void main(String args[])

    is fine, right? I know it's common, but is it correct? I don't
    need 'int' before the 'main', right?

    Chuck Heathfield
    Chuck Heathfield, Feb 3, 2009
    1. Advertisements

  2. public static void main(String[] args)
    or the varargs variant:
    public static void main(String... args)

    are the methods that the Java VM will attempt to invoke when passing in
    the class name to java. Any other signature (differing return value,
    differing name in case, lack of static, lack of public access, incorrect
    argument type and/or count) will not be recognized by the VM.

    Naturally, the method must be enclosed in a class.

    Finally, why is your followup-to set to c.l.c, which ain't even in your
    newsgroup list?
    Joshua Cranmer, Feb 3, 2009
    1. Advertisements

  3. Chuck Heathfield

    Lew Guest

    For many basic questions about Java programming, basic answers can be found in:
    As Joshua illustrates here, it is idiomatic in Java to place the square
    brackets after the type ('String') rather than the variable ('args') in a
    declaration, except when there are specific reasons to do the other.
    Quoted nearly verbatim from the JLS. Good show.
    For answers to this kind of question, go right to the defining specification:
    I never even saw the original post until the response quoted it.
    Lew, Feb 3, 2009
  4. What reasons might those be? AFAIK, it's always best to put the
    square brackets after the type.
    Mike Schilling, Feb 3, 2009
  5. Chuck Heathfield

    Lew Guest

    I've seen it done with "multi-dimensional" arrays where the first /n/
    dimensions are specified:

    int matrix [] [] = new int [5] [5];

    I make sense of this by thinking that the dimensionality inheres to the
    instance, pointed to my 'matrix', as a specific implementation, not a type.

    It's also useful for declaring multiple related items in one line:

    int scalar, matrix [] [];

    That assumes you don't excoriate multiple declarations per line.

    Since Java allows both positions, the reasons to choose one are convention,
    clarity and documentation of intent. To my mind, putting the brackets with
    the type:
    int [] [] matrix;
    documents the type, the other way documents the item. Normally I intend
    source to be read in terms of types, so I'll favor bracketing the type, but
    sometimes the sense and purpose of the code seems clearer the other way.
    Lew, Feb 3, 2009
  6. Chuck Heathfield

    Daniel Pitts Guest

    So, I wrote this reply before realizing the f-u was set to comp.lang.c.
    It seems like this may have been an attempt to troll. However, giving
    the benefit of the doubt, here is the answer:

    In Java, it must always be
    public static void main (String[] args) // Pre 1.4 preferred
    public static void main (String...args) // 1.5 or later preferred
    public static void main (String args[]) // Never preferred, but valid

    You might be confusing Java main with C/C++ main, where void is not
    valid (but sometimes works anyway).

    In C/C++ the correct signature would always be:

    int main(int argc, char**argv);
    Daniel Pitts, Feb 3, 2009
  7. Chuck Heathfield

    Mark Space Guest

    In my opinion, it's because the type of the (array) object is String[],
    so it's done for consistency and clarity.

    int[] ia = {1, 2, 3, 4};
    List<int[]> ial = Arrays.asList( ia );

    Class<?> type = int[].class;

    Works the same as it does for other class objects.
    Mark Space, Feb 3, 2009
  8. [...]

    No benefit of the doubt is warranted. This is part of a pattern of
    abusive messages. If you see a message with followups inappropriately
    set to comp.lang.c, please ignore it. (I won't bore you with the
    other header information that indicates the original poster is a
    Keith Thompson, Feb 3, 2009
  9. Chuck Heathfield

    Arne Vajhøj Guest

    Java is not C.

    You use void main in Java and int main in C.

    Arne Vajhøj, Feb 3, 2009
  10. Chuck Heathfield

    Arne Vajhøj Guest

    I would use String[] even at 1.5+ because it is more
    traditional syntax and it will (practically) never be used
    from Java with a variable number of arguments.

    Arne Vajhøj, Feb 3, 2009
  11. Chuck Heathfield

    Zach Guest

    Please stop posting this stuff in comp.lang.c, the topic of this
    newsgroup is C not Python!

    Zach, Feb 4, 2009
  12. Chuck Heathfield

    Arne Vajhøj Guest

    You think Daniel is posting Python in ?

    Tsk tsk !

    Arne Vajhøj, Feb 4, 2009

  13. Really? I think it could be done so quite often, by a test program
    emulating a user who types in different numbers of arguments, or
    includes and omits flags, etc.
    Mike Schilling, Feb 4, 2009
  14. Chuck Heathfield

    Arne Vajhøj Guest

    Good point.

    Maybe I have just been doing too little console app programming.

    Arne Vajhøj, Feb 4, 2009
  15. I've been doing quite a lot and using pretty much the gamut of argument
    types (file names, options, options with values, both long [--optname]
    and short [-o] option naming conventions) and have yet to find anything
    that can't be easily handled by a 'String[] args' argument declaration.
    Martin Gregorie, Feb 5, 2009
  16. Chuck Heathfield

    Tom Anderson Guest

    The point of String... is not to change the way you implement main - it
    makes absolutely no difference at all - but to make it more convenient to
    call main from other code. For instance, if you want to write a test
    harness, you can do:

    testMain() {
    MyApp.main("destroy", "noc-list.txt");

    Rather than:

    testMain() {
    MyApp.main(new String[] {"destroy", "noc-list.txt"});

    It's a modest improvement.

    Tom Anderson, Feb 5, 2009
  17. This question is only tied to whether you ever *call* main-methods from
    your own code. If you do, then declaring main(String... args)
    means, that you can call it from Java code as such:
    SomeProgram.main("-o", "foobar", "--optname", "snafu", filename);
    (and the compiler creates the String[] for you)

    If you declared it with String[] args, you'll have to create the
    String array exlicitly (as you always had to do in pre-1.5 Java).

    If you never call main-methods from your own Java code, then you
    won't ever notice the difference for main-methods - definitely not
    from main()'s implementation.
    Andreas Leitgeb, Feb 5, 2009
  18. Chuck Heathfield

    Lew Guest

    Interestingly, in

    and the followup to that message was set to comp.lang.c.

    A spoofer?
    Lew, Feb 5, 2009
  19. I missed that, probably because I've never tried to call main() from
    another program.

    I don't leave testing code in classes, so missed that use too.

    The closest I've come was to write a "Launcher" class to select the
    program to run from the application's jar file. Even there String[] was
    fine since the launcher merely loads the main class, named in args[0],
    and passes args[1]-args[args.length] to the class its just loaded.
    Martin Gregorie, Feb 5, 2009
  20. Chuck Heathfield

    Lew Guest

    I apologize for not recognizing that more quickly.

    Several of us here in the clj groups had someone spoofing our posts a
    few months back, somewhat more viciously but equally annoyingly. Some
    contributors suggested that Usenet needs a digital-signature scheme to
    prevent such spoofs.

    I conclude that you (the real Keith) are a worthwhile newsgroup
    contributor, to have attracted the attention of a spoof-troll.

    Keep up the good work.
    Lew, Feb 5, 2009
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.