1

In all of my life, I have been wondering what do these two differ from each other, the difference in

public static void main (String[] args){}

and

static public void main(String... args){}

I have seen these code with my friend but even him doesnt know the reason.

How do these two differ from each other?

sorry i edited the return type of the main method...

Spurdow
  • 2,025
  • 18
  • 22
  • 5
    Both are incorrect since there is no return type – Mohayemin Sep 26 '13 at 10:23
  • See http://docs.oracle.com/javase/7/docs/technotes/guides/language/varargs.html –  Sep 26 '13 at 10:24
  • 3
    You must be very young. You've only had 18 years to worry about it. The order of 'static' and 'public' is immaterial to the compiler, as a few minutes with the Java Language Specification would have told you. – user207421 Sep 26 '13 at 10:25
  • @mohayemin thanks for pointing it out to me. i was really confused when i was typing it. – Spurdow Sep 26 '13 at 10:30

6 Answers6

6

There is no difference in functionallity in these two. But in respect of conventions use the visibility operators, like public, private, first followed by static.

And don't forget the return type!

This is how it should look:

public static void main (String[] args){}

The parameter String[] args is a normal array. String... args are called varargs. In this case there is no difference. But have a look here about varargs.

Steve Benett
  • 12,843
  • 7
  • 59
  • 79
4
public static void main (String[] args){}
static public void main (String... args){}

are the same. Just note that return type (in this case void) cannot be repositioned.

See reference.

Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
  • hey can u please answer my question posted in here? http://stackoverflow.com/questions/18838331/paypal-mobile-integration-android-and-libgdx seems like i can trust you with many problems regarding about libgdx. – Spurdow Sep 26 '13 at 13:45
  • @Spurdow It's illegal to use paypal api for ios/android IAP. You MUST use respective IAP api's provided to you via play store/iTunes. If your country is not listed as merchant, you cannot do anything about it but wait. You can check with Soomla/Swarm. They do it some other ways but there's a risk. – Sajal Dutta Sep 26 '13 at 13:49
2

The only difference between the two is the way you call the function. With String var args you can omit the array creation.

public static void main(String[] args) {
    callMe1(new String[] {"a", "b", "c"});
    callMe2("a", "b", "c");
    // You can also do this
    // callMe2(new String[] {"a", "b", "c"});
}
public static void callMe1(String[] args) {
    System.out.println(args.getClass() == String[].class);
    for (String s : args) {
        System.out.println(s);
    }
}
public static void callMe2(String... args) {
    System.out.println(args.getClass() == String[].class);
    for (String s : args) {
        System.out.println(s);
    }
}  

Here is the link

This is functionally same

  public static 

or

  static public 
  1. varagrs
  2. when do you use varargs
  3. the best explained tutorial varargs
Community
  • 1
  • 1
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
0

You can write both. It doesn't matter. It means exactly the same.

The same goes for variables. You can write

final private String

or

private final String
Shervin Asgari
  • 23,901
  • 30
  • 103
  • 143
0

Both are same, It's just a matter of personal style. Conventional is providing accessibility first

public static void main(String... args)
user207421
  • 305,947
  • 44
  • 307
  • 483
Siva
  • 1,938
  • 1
  • 17
  • 36
0

Both are same, as many have already written. I just want to point out that the varargs feature in Java was implemented from Java 5 (Java 1.5) version. So you just have to pay attention to this using the "varargs style".

Paolo
  • 1,641
  • 11
  • 15