Can you pass a new array as a method, and still have data in that array?
For example, I have this method: foo(String[]), and i want to call it like thisfoo(new String[] s = {"String1", "String2"}). But that's not valid Java.
This is a "valid Java way" (as in, it compiles and does what you want):
foo(new String[] {"String1", "String2"});
If you have the opportunity to change this method, then you can also consider to change the method to take a varargs argument:
public void foo(String... strings) {
// ...
}
Then you can use it as follows without the need to explicitly create an array:
foo("String1", "String2");
The following is then also valid:
foo("String1");
and
foo("String1", "String2", "String3");
and even more.
If you're trying to make a local variable s and keep it around, no, you can't declare a variable within a method call. You have to declare it first outside of the call:
String[] s = {"String1", "String2"};
foo(s);
You could initialize s within the call, but I don't see any reason to do so, plus it looks really bad:
String[] s;
foo(s = new String[] {"String1", "String2"});