3

I'm trying to pass all command line arguments to a method. I get an error on String a = method(args[]);.

public class Test {
    public static void main (String[] args){
        String a = method(args[]);
        System.out.println(a);
    }

    static String method (String[] args){
        String b = args [0];
        return b;
    }
}
1
  • 5
    it's method(args); Commented Feb 20, 2016 at 10:54

3 Answers 3

3

Just remove the [] brackets. They are part of the type, not the parameter's identifier:

String a = method(args);


Type: String[] ("array of strings")

Parameter name: args

Sign up to request clarification or add additional context in comments.

Comments

1

Replace String a = method(args[]); with String a = method(args);

You do not have to include [] while passing the array.

Comments

1

In your case args is already an array, so you just need to pass it in the method.

String a = method(args[]); this is wrong

String a = method(args); // remove []

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.