0

Possible Duplicate:
how to use (String [] [] args) in java

Can we use public static void main(String [] [] args) in Java for 2d arrays?

Community
  • 1
  • 1
yasitha
  • 709
  • 2
  • 6
  • 12
  • 1
    There were five answers on your other post: http://stackoverflow.com/questions/4860165/how-to-use-string-args-in-java In what way didn't those five answers address your _actual_ question? (I don't know what you're _trying_ to ask, but it seems clear English isn't your native language; I hope you can be more detailed about what you want.) – sarnold Feb 01 '11 at 08:39
  • I think this question is different - it asks about the main method. – Bozho Feb 01 '11 at 08:40
  • Maybe you can try explaining in your language and someone could translate in english for us. – bluish Feb 01 '11 at 08:41
  • @Bozho, the OP explicitly said in one of the comments in the previous question that it was about main() too, it just had wrong wording. So this should have been an edit to the previous question. – Sergei Tachenov Feb 01 '11 at 09:51

6 Answers6

5

There are three possible ways you can define the parameters of the main method:

  1. Classic Java Style

    public static void main(String[] args)
    
  2. C Style

    public static void main(String args[])
    
  3. New-school (post JDK1.5) Java style

    public static void main(String ... args)
    

All of these are equivalent, and the VM will only start your class if it finds a method with one of these signatures.


Actually, they are not quite equivalent, there is one small difference: When accessed via reflection, Method.isVarArgs() will only return true for the last method. But they still all have an equal signature (name, parameter types, return type, visibility)

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
4

No you can't. How would you specify command-line arguments to fit in a 2d array? The main(..) method is invoked by the java runtime which passes the command-line arguments specified when the program is invoked.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
3

Sure, you can declare your own main method that takes a 2d array, but it won't be executed automatically by the JVM on startup, because it looks for a method with a signature:

public static void main(String[] args)

See the Hello World tutorial for more information on the main method.

dogbane
  • 266,786
  • 75
  • 396
  • 414
1

The two main facts pointed out in other questions:

  • You can have a method with that signature.
  • It can't be used as an entry point.

So if you really want to have an entry point that accepts a 2D array, you should do this:

public static void main(String[] args) {
   String[][] args2d;
   // some crazy code that parses args and initializes args2d with a 2D array
   main(args2d);
}

public static void main(String[][] args) {
   // your actual entry point here
}

Note that you don't have to declare the second method public in this case, but you still can do it if you really want to.

Sergei Tachenov
  • 24,345
  • 8
  • 57
  • 73
0

no you can't.because main(String[] args) method is special method for JVM and it's looking for

main method with this signature :

public static void main(String[] args) {
}
mehdi shahdoost
  • 1,469
  • 5
  • 17
  • 27
0

No two dimensional array will not work.When you are using String args[] means you are going to send array of string arguments to the compiler but you cant use a two dimensional array. You are going to get Class not found exception.But the file will compile.

sush
  • 5,897
  • 5
  • 30
  • 39