7

Is there a way to use doubleColon to invoke second level method. Example ClassA::getClassBObject::classBMethod in lambda invocation

Ravisha
  • 3,261
  • 9
  • 39
  • 66

2 Answers2

7

What you are expecting is called chaining. Method references does not support chaining.

Read more about Method References.

Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67
7

You could compose the functions:

((Function<ClassA, ClassB>) ClassA::getClassBObject).andThen(ClassB::classBMethod)

But you're probably better off with just (x, arg) -> x.getClassBObject().classBMethod(arg).

More info on the composition approach: https://stackoverflow.com/a/32838924/2093695

Brian McCutchon
  • 8,354
  • 3
  • 33
  • 45
  • no need to downvote. He mentioned that it is a solution but not a good one – Ravisha May 08 '20 at 06:26
  • @roundAbout I posted it because it's interesting, not because it's a good idea in real code. If anything, I think my answer shows by counterexample why you should just use a regular lambda. – Brian McCutchon May 08 '20 at 06:45