Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing string array values directly to setter method

There's a String array that I have which has setter and getter methods. How do I directly pass values {"one", "two"} to the setter method rather than setting the values to a variable first and then passing the parameter?

String[] arr1 = {};
public String[] getArr1() {
    return arr1;
}

public void setArr1(String[] arr1) {
    this.arr1 = arr1;
}

..expecting something like setArr1(?);...

like image 250
Mercenary Avatar asked Dec 20 '22 11:12

Mercenary


1 Answers

You could use setArr1(new String[]{"one", "two"})

Alternatively, you could make use of varargs and change your method signature to setArr1(String... values) and use the method as setArr1("one", "two")

like image 169
Scorpion Avatar answered Dec 23 '22 02:12

Scorpion