I have following piece of code
StringJoiner joiner = new StringJoiner(", ");
joiner.add("Something");
Function<StringJoiner,Integer> lengthFunc = StringJoiner::length;
Function<CharSequence,StringJoiner> addFunc = StringJoiner::add;
Last line cause an error
Error:(54, 53) java: invalid method reference
non-static method add(java.lang.CharSequence) cannot be referenced from a static context
I understand that this method can't be used in static way and I should have something like :
Function<CharSequence,StringJoiner> addFunc = joiner::add;
instead. However I can't understand why third line, with StringJoiner::length;
is for java compiler perfectly correct. Can someboedy explain me why is that ?