Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why public void static main(String args[]) gives compilation error?

 
Har Kaur
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried changing replacing the positions of static and void in declaration of main and it produced a compiler error.
Can anyone tell me why compiler treats this as an error?

-Har
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the exact syntax for any method is this:
<modifiers> <return type> <name>(<parameter list>)

The modifiers (public, static, but also final, synchronized, etc) can be in any order, but the return type (void) must come between them and the method name.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi and welcome to the JavaRanch.

Beside the previous remarks I would like to add that it is advisable to declare the variable as "String[] args" not as "String args[]".
This is a widely used convention in the Java world.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason for that convention is that the correct type of "args" is String[], ie array of Strings. Not String, which is converted to an array. So you should write String[] args, not String args[], even though the compiler will accept both forms.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allowing the [] to be put after the variable name is a left-over from C/C++, which the Java designers copied to make it easier for C/C++ programmers to switch to Java. A mistake in my opinion, because of what Campbell said - the [] belong to the variable type (String), not the variable name (args).
 
Har Kaur
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Because the exact syntax for any method is this:
<modifiers> <return type> <name>(<parameter list>)

The modifiers (public, static, but also final, synchronized, etc) can be in any order, but the return type (void) must come between them and the method name.


Thanks Rob. This cleared many doubts.
 
Har Kaur
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob and Campbell,

Thanks for correcting the mistake.

But how is the data structure or memory allocation different for the two. How is array of Strings different from a String converted to an array.
I tried the below code and didn't found any difference
String test[]= {"a","b"};
String[] test1 = {"a","b"};


I will highly appreciate if you can explain the limitations/differences between both the declarations using an example.
Sorry if this is a naive question

Also when I tried
System.out.println(test);
System.out.println(test1);


It gave the following output
[Ljava.lang.String;@10b62c9
[Ljava.lang.String;@82ba41


Can anyone explain me this?
 
Angus Comber
Ranch Hand
Posts: 90
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume Rob and Campbell were simply mentioning that both forms are allowed by the compiler but String[] test1 is more expressive of the intent. ie reading from left to right, A String array (read [] as indicating an array) named test1.

 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. Both are indeed allowed, but most Java programmers prefer "String[] test" over "String test[]", and Angus' explanation shows exactly why.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want an explanation why you get [Ljava.lang.String@abcd1234or similar, start here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic