Reputation: 274
Is it possible to call a method multiple times in a row and pass in different arguments each time, but without repeating the method name when calling?
For example, in JavaFX, you can add nodes to another node by calling the getChildren.add()
method of the parent node each time:
GridPane gridPane = new GridPane();
Button button = new Button();
Label label = new Label();
Checkbox checkbox = new Checkbox();
gridpane.getChildren.add(button);
gridpane.getChildren.add(label);
gridpane.getChildren.add(checkbox);
As you can see, it can get quite repetitive. Is it possible to make the calls without repeating the method name? I would like to know if I could do something like:
GridPane gridPane = new GridPane();
Button button = new Button();
Label label = new Label();
Checkbox checkbox = new Checkbox();
gridPane.getChildren.add {
button;
label;
checkbox;
}
Please note that I am not suggesting the syntax above to be added into the Java language. It was just written to help clarify my point.
Upvotes: 2
Views: 1495
Reputation: 7290
That's not supported in Java's syntax. The philosophy of Java mainly is to express your program quite explicitly. So, if you want three method calls, you have to write down three method calls (or introduce some kind of loop or use streams).
To explain why such a syntax isn't part of Java, I'll discuss your idea as a potential language extension to Java where something like
m {{
"foo";
"bar";
"foobar";
}};
has the same meaning as
m("foo");
m("bar");
m("foobar");
If asked about such a multi-call feature, I'd definitely vote against its introduction into Java. Why:
m{{...}}
multi-call expression return all the values as an array? As a List? Only the last call's return value? None of them? If you use the classical style, it's easy for developers to choose the treatment of the return values. With multi-call, they are bound to only one possible version, the one that became part of the language definition.I guess that the people responsible for Java's future will at least share some of my doubts, so I bet such a feature will never make it into Java.
Upvotes: 0
Reputation: 11496
void m(String value) {
System.out.println(value);
}
void m(String[] values) {
for (String value : values) {
m(value);
}
}
Usage:
m("foo") // foo
m("bar") // bar
m({"foo", "bar"});
// foo
// bar
void m(String... values) {
for (String value in values) {
System.out.println(value);
}
}
Usage:
m("foo") // foo
m("bar") // bar
m("foo", "bar");
// foo
// bar
Upvotes: 1
Reputation: 27119
There's no syntax similar to your example. The closest you can get for the same effect is either with loops (like in zazz's answer) or with the Stream API.
Depending on whether you care about the return value or not, something like
List.of("foo", "bar", "foobar").forEach(x -> m(x));
or
Stream.of("foo", "bar", "foobar").map(x -> m(x)).collect(Collectors.toList());
Upvotes: 2
Reputation: 1
I don't think it is possible but you can put all the desired string in an array then do a for loop :
String[] arr ={"foo","bar","foobar"};
for(int i=0;i<3:i++){
m(arr[i]);
}
Upvotes: 2