aspose file tools*
The moose likes Beginning Java and the fly likes Why public void static main(String args[]) gives compilation error? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why public void static main(String args[]) gives compilation error?" Watch "Why public void static main(String args[]) gives compilation error?" New topic
Author

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

Har Kaur
Greenhorn

Joined: Jul 22, 2011
Posts: 4
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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38780
    
  23
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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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

Joined: Jul 22, 2011
Posts: 4
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

Joined: Jul 22, 2011
Posts: 4
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

Joined: Jul 16, 2011
Posts: 90
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

Joined: Oct 27, 2005
Posts: 19693
    
  20

Correct. Both are indeed allowed, but most Java programmers prefer "String[] test" over "String test[]", and Angus' explanation shows exactly why.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38780
    
  23
If you want an explanation why you get [Ljava.lang.String@abcd1234or similar, start here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why public void static main(String args[]) gives compilation error?