0

Please keep in mind that this is a general question. I don't want to solve this problem only with arraylists, instead I want to know if there is a general solution for this. I know Arrays.asList(...) would do it here. The solution must also be applicable for existing classes without rewriting all setters to builder.

Sometimes I need a object only in a single line but in order to pass the objects' reference I need to declare a variable. I want to show you the following general example:

Person.addPhoneNumber((new ArrayList<String>()).add("0123456789").add("9876543210"));

This isn't working, because add() returns a boolean, so I lost my ArrayList reference.

  • Does the Builder pattern solve your problem here? – Glains Oct 07 '18 at 14:36
  • @Glains Am I right that the builder pattern returns "this" in all setters? When I'm right, this is not applieable here, because I had to override all ArrayList methods. I need a solutions for classes that already exist – Philipp Niedergesäß Oct 07 '18 at 14:43
  • The short answer is no. There is no general-purpose way to call multiple `void` methods in a single logical line of code. – Daniel Pryden Oct 07 '18 at 15:07

0 Answers0