Is there a way to use doubleColon to invoke second level method.
Example ClassA::getClassBObject::classBMethod
in lambda invocation
Asked
Active
Viewed 1,551 times
7

Ravisha
- 3,261
- 9
- 39
- 66
-
Did you try ClassA.getClassBObject()::classBMethod? – JavaTechnical May 08 '20 at 06:07
-
Does [this answer](https://stackoverflow.com/questions/29146199/java-8-chained-method-reference) your question? Summary: no, duplicate method references are not allowed – Nowhere Man May 08 '20 at 06:12
-
@JavaTechnical that simply doesnt work. getClassBObject isnt a static method – Ravisha May 08 '20 at 06:28
-
@Ravisha I have posted as an example, if it isn't a static method, use the reference instead. – JavaTechnical May 08 '20 at 06:31
-
@JavaTechnical that is the whole point of this question isn't it? – Ravisha May 08 '20 at 06:43
2 Answers
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