Scrivio Interview library
DebuggingIntermediate5 min read · updated 12 November 2026

This compiles in Java 8 but a teammate's refactoring to a chained call fails to compile:

Java
Comparator<User> c = Comparator.comparing(u -> u.getName()); // ok
Comparator<User> d = Comparator.comparing(u -> u.getName()).reversed(); // error

Explain and fix.

Target typing only reaches the outermost expression. In Comparator.comparing(u -> u.getName()).reversed(), the comparing call is the receiver of reversed(), not the value assigned to Comparator<User>, so it has no target type, its type parameter T falls back to Object, and u.getName() cannot be found on Object. We fix it with a method reference, Comparator.comparing(User::getName).reversed(), an explicit lambda parameter type, (User u) -> u.getName(), or explicit type arguments, Comparator.<User, String>comparing(u -> u.getName()).reversed().

Why this question is asked

This bug appears constantly when people refactor sorting code: a line that compiled for years stops compiling as soon as .reversed() or .thenComparing(...) is added. The fix is easy to find by trial and error, but interviewers want the reason, because it shows we understand how Java infers lambda types and why inference does not flow backwards through a method chain. In this answer, we will reproduce the error, see how the first line gets its types, see why the chained line loses them, apply the three fixes, and explain why the method reference works.

1Reproducing the Error

Here are the two lines from the question, with the error javac reports.

Java
record User(String name, int age) { String getName() { return name; } }
Comparator<User> c = Comparator.comparing(u -> u.getName()); // ok
Comparator<User> d = Comparator.comparing(u -> u.getName()).reversed(); // error
// error: cannot find symbol
// symbol: method getName()
// location: variable u of type Object

Notice the last line of the message: the compiler thinks u is an Object. That is the whole story in one phrase, and the question is where it got Object from.

2How the First Line Gets Its Types

Comparator.comparing is a generic method: static <T, U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor). The lambda u -> u.getName() has no declared parameter type, so it is an implicitly typed lambda, which needs a target type to know what u is.

On the first line, the whole call comparing(...) is assigned to Comparator<User>. That is its target type. The compiler reasons backwards: the method returns Comparator<T>, the target is Comparator<User>, so T = User. Then the lambda's target is Function<? super User, ? extends U>, so u is a User, u.getName() returns String, and U = String. Everything is resolved.

Step throughInference on the line that compiles
  1. Target type Comparator<User>from the assignment
  2. comparing returns Comparator<T>so T = User
  3. Lambda target Function<User, U>so u is a User
  4. u.getName() returns Stringso U = String
  5. Call fully typedcompiles

5 steps in order. Play them, or step through them one at a time.

3Why the Chain Loses the Target

On the second line, the expression assigned to Comparator<User> is the whole chain, and the last call in it is reversed(). reversed() is an instance method of Comparator<T>, and to know which reversed() to call, the compiler first needs the type of the receiver, comparing(...). It must type the receiver on its own, before it looks at the rest of the line.

Java's inference is deliberately local: a method call that is the receiver of another call is not a poly expression, so it gets no target type from what happens later in the chain. With no target, nothing constrains T in comparing(...) except the lambda, and an implicitly typed lambda cannot provide its own parameter type. So T falls back to its bound, Object, u becomes an Object, and u.getName() does not exist. The assignment target Comparator<User> only reaches reversed(), which is too late.

Step throughWhere the type information stops
Assignmentreversed()comparing(...)lambda u
target type Comparator<User>
needs receiver type first, passes no target
T has no constraint: T = Object
u is Object
u.getName(): cannot find symbol

5 messages in order. Play them, or step through them one at a time.

The target type stops at the outermost call; the receiver is typed alone.

4Three Ways to Fix It

Each fix gives the compiler the missing type in a different place.

Java
// Fix 1: a method reference
Comparator<User> d1 = Comparator.comparing(User::getName).reversed();
// Fix 2: an explicitly typed lambda
Comparator<User> d2 = Comparator.comparing((User u) -> u.getName()).reversed();
// Fix 3: explicit type arguments on the generic method
Comparator<User> d3 = Comparator.<User, String>comparing(u -> u.getName()).reversed();
List<User> users = new ArrayList<>(List.of(new User("ann", 30), new User("bob", 25)));
users.sort(d1);
System.out.println(users.get(0).name()); // bob: names in reverse order

Fix 2 declares u's type in the lambda itself, so the lambda no longer needs a target and T = User follows from it. Fix 3 tells the method its type arguments directly; it works, but it is the noisiest. Splitting the line also works: assign comparing(...) to a Comparator<User> variable first, then call reversed() on it, which gives the inner call back its target.

5Why the Method Reference Works

A method reference such as User::getName is an exact method reference when the method is not overloaded: it names one specific method in one specific class. The compiler can use it as information, not only as something to be typed. From User::getName it learns that the function takes a User and returns String, so T = User and U = String even with no target type at all.

An implicitly typed lambda carries no such information, because u -> u.getName() could mean any type with a getName() method. That is the difference: the method reference carries its own type, and the implicit lambda borrows its type from the context. In a chain, there is no context to borrow from. Note that if getName were overloaded, the method reference would be inexact and could fail the same way, in which case fix 2 or fix 3 is needed.

6Follow-up questions

Why doesn't the compiler just look at the whole chain?

Because inference across a chain would need the receiver's type to depend on methods called on it later, and those methods can only be found once the receiver's type is known. That is circular. Keeping inference local to each call keeps the rules decidable, compile times predictable and error messages tied to one expression.

Does thenComparing have the same problem?

Yes. comparing(u -> u.getName()).thenComparing(u -> u.getAge()) fails for the same reason, because the first call has no target. With method references, Comparator.comparing(User::getName).thenComparing(User::getAge) works.

Is Comparator.comparing(User::getName, Comparator.reverseOrder()) an alternative?

Yes, and it avoids the chain entirely: the second argument is a key comparator, so it sorts by name in reverse order. It is a good choice when only the key ordering should be reversed.

7Conclusion

In this answer, we saw why adding .reversed() breaks Comparator.comparing(u -> u.getName()). Target typing only flows to the outermost expression, and in a chain the comparing call is a receiver that is typed alone, so its T becomes Object and the implicit lambda's parameter u is an Object without getName(). Using a method reference, an explicitly typed lambda or explicit type arguments supplies the missing type, and the method reference works because it carries its own parameter and return types.

What to say in the interviewAbout 80 seconds aloud

Open with

The first line works because the assignment gives comparing a target type: Comparator<User> means T is User, so the lambda's parameter u is a User and getName resolves.

Then, if they want more

In the second line, the expression assigned is the result of reversed(), and comparing(...) is just the receiver of reversed. Java's inference is local, so a receiver expression gets no target type from the rest of the chain; the compiler has to type comparing on its own, before it can even find reversed.

With no target, the only input is the lambda, and an implicitly typed lambda cannot tell the compiler its parameter type, so T falls back to Object, u is an Object, and the error is cannot find symbol getName in Object. There are three fixes.

The best is a method reference, Comparator.comparing(User::getName).reversed(), which works because User::getName names one exact method, so the compiler learns T is User and the key is String from the reference itself. The second is an explicit lambda type, (User u) -> u.getName(). The third is explicit type arguments, Comparator.<User, String>comparing(...). Splitting into two statements also works. The same thing happens with thenComparing.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "This compiles in Java 8 but a teammate's refactoring to a chained call fails to compile:": https://scrivio-interview-library.netlify.app/java/generics/545a1246ba/