Scrivio Interview library
LeadershipPrincipal7 min read · updated 12 November 2026

Ten years from now your codebase will have been written across Java 8 to Java 25 and beyond. Looking at features such as var, diamond and inferred lambda types, how do you decide how much type information code should state explicitly, and how do you keep that decision consistent across hundreds of engineers?

Type inference saves the writer a few keystrokes, but it can cost every later reader time when the type is not obvious from the line itself, and code is read many more times than it is written. So I keep explicit types at API boundaries, which Java already requires for fields, parameters and return types, allow var, diamond and inferred lambda parameters inside methods when the type is clear from the right-hand side, and make that a short written rule with examples, enforced by IDE inspections and review, revisited when Java adds new inference features, and judged by maintenance outcomes rather than taste.

Why this question is asked

Over a long life a Java codebase collects every style the language has allowed. Code from Java 8 spells out Map<String, List<Order>> twice, code from Java 10 onwards may use var everywhere or nowhere, and newer code mixes records, patterns and lambdas with inferred types. Interviewers ask this at a senior level because the technical question is easy and the organisational question is hard: how do we make a decision that hundreds of engineers will apply the same way, for years, without endless review arguments? In this answer, we will recap what each inference feature does, look at the real trade-off between writing and reading, set out where explicit types belong, turn that into a rule people can follow, and decide how to keep it consistent and up to date.

1A Quick Recap of Type Inference in Java

Type inference means the compiler works out a type we did not write. Java has added it in small steps. Java 7 brought the diamond operator, so new ArrayList<>() takes its type argument from the variable it is assigned to. Java 8 brought lambdas, whose parameter types are inferred from the functional interface they implement. Java 10 brought var for local variables, where the type is taken from the initialiser, and Java 11 allowed var on lambda parameters so we can put annotations on them.

Notice what all of these have in common: they only apply inside a method body or an expression. Java never infers the type of a field, a method parameter or a return type. That boundary matters for our rule, because the places Java forces us to be explicit are exactly the places other code depends on.

Java
Map<String, List<Order>> byCustomer = new HashMap<>(); // diamond: HashMap<String, List<Order>>
var orders = new ArrayList<Order>(); // var: ArrayList<Order>
orders.removeIf(o -> o.total() < 0); // lambda: o is an Order
var result = service.process(orders); // var: ??? the reader cannot tell

The last line is the whole debate in one statement. The compiler knows exactly what process returns, and so does the IDE when we hover over it. A reviewer reading the diff in a browser, or someone reading a stack trace at three in the morning, does not.

2Writing Cost Against Reading Cost

Every feature above lowers the cost of writing code. The question is what it does to the cost of reading it. When the type is visible on the same line, as in var orders = new ArrayList<Order>(), repeating it adds noise and var helps the reader. When the type comes from a method whose name does not reveal it, var hides information the reader needs to understand what can be done with the variable.

So the useful test is not whether inference is allowed, but whether a reader without an IDE can tell the type from the line. Code review tools, merge request diffs and printed logs are all places without hover, and senior engineers spend a lot of time in exactly those places. Code is typically read many times for each time it is written, so a small reading cost multiplied over the life of the code outweighs the writing saving.

Where inference helps and where it hurts
CaseInferenceReason
var list = new ArrayList<Order>();allowtype is on the same line
var in = Files.newBufferedReader(p);allowfactory name says the type
for (var entry : map.entrySet())allowlong generic type, obvious role
var result = service.process(x);prefer explicitreader cannot tell the type
var count = 0; then count += 1.5avoidint inferred; += casts 1.5 back to int
field, parameter, return typenot possibleJava requires explicit types here
The rule follows the reader: inference where the type is visible, explicit types where it is not.

3Keep Explicit Types at the API Boundary

The first and easiest part of the policy costs nothing, because Java already enforces it: fields, parameters and return types are always explicit. We should go further in the same direction and avoid weakening those boundaries in other ways, for example by returning var-friendly but vague types such as Object or raw collections. A public method signature is a contract that many callers depend on, so it should say exactly what it gives and takes.

Inside a method, the scope is small and the reader can see the whole context. That is where inference earns its place. A method of 20 lines that uses var for obvious local types is easier to read than the same method with every generic type spelled out twice. But there is a catch: var with numeric literals and with interfaces can quietly pick a narrower or more concrete type than intended. var count = 0 is an int, and var list = new ArrayList<Order>() is an ArrayList, not a List, which matters if someone later assigns a different implementation.

4Turning Judgement Into a Rule People Can Follow

Hundreds of engineers will not apply a principle like reading cost the same way. They will apply examples. So the rule should be one short page in the engineering guide: a sentence of principle, then six or eight pairs of good and bad snippets taken from our own code, like the table above. Engineers copy examples far more reliably than they interpret prose.

Then we automate what can be automated. IntelliJ IDEA and other IDEs have inspections for local variable type style, and a shared inspection profile checked into the repository makes every developer see the same hints. Formatters such as google-java-format do not change var usage, so the formatter alone is not enough; static analysis rules in Checkstyle, PMD or Sonar can flag the clear cases, and check your tool versions for exactly which rules exist. Whatever cannot be automated, such as whether a method name reveals its type, stays a review judgement, and the guide gives reviewers a link to point at instead of arguing from personal taste.

Step throughHow a team keeps one rule on inference
  1. Write the principlereaders without an IDE can tell the type
  2. Add 6 to 8 examplesfrom our own code, good and bad
  3. Automate the clear casesshared IDE profile, lint rules
  4. Review the restreviewers link to the guide
  5. Collect outcomesreview comments, onboarding feedback
  6. New inference feature in Java?
    YesUpdate the guide centrallyNoKeep the rule as it is

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

The rule is owned centrally and changes with the language, not team by team.

5Keeping the Decision Current for Ten Years

Java will keep adding features that infer types, as it did with var, record patterns and unnamed variables. If each team decides alone what to do with a new feature, we get the inconsistency we started with. So the guide needs an owner, typically a small group of staff engineers, who reviews it when each LTS release lands, for example when a team asks to move to Java 25, and publishes one update for everyone.

We also judge the rule by outcomes rather than opinions. Useful signals are the number of review comments about type clarity, onboarding feedback from new joiners about code they found hard to follow, and how often reviewers ask what a variable is. If those go up after we relax the rule, we tighten it again. Old code is not rewritten to match: we apply the rule to code people touch, which keeps the diff small and the history readable.

6Follow-up questions

Would you ban var entirely to keep things simple?

No. A ban is easy to enforce, but it throws away a feature that genuinely improves readability for long generic types and obvious constructor calls. It also tends to fail socially, because engineers who see the benefit start ignoring the rule, and then nothing is consistent. A narrow rule with good examples is more stable than a ban.

What about var with the diamond operator?

Combining them is a trap. var list = new ArrayList<>() infers ArrayList<Object>, because there is nothing left for the diamond to take its type from. So the guide should say that with var, the type argument goes on the constructor, as in var list = new ArrayList<Order>().

How do you handle disagreement from a strong senior engineer?

I ask them to bring examples from our code where the rule makes things worse, and we test the rule against those examples together. If they are right, the rule changes for everyone. If not, the written rule and its reasons stand, and the disagreement is recorded. The point is that the rule changes through one visible process, not through individual exceptions in review.

7Conclusion

In this answer, we saw that type inference trades lower writing cost for possibly higher reading cost, and that the useful test is whether a reader without an IDE can tell the type from the line. We keep explicit types at API boundaries, which Java requires, allow inference inside methods where the type is visible, write the rule as a short guide with examples from our own code, automate the clear cases, review the guide when new Java features arrive, and judge it by maintenance outcomes rather than taste.

What to say in the interviewAbout 100 seconds aloud

Open with

I start from the fact that code is read far more often than it is written, so inference is good when it removes noise and bad when it hides a type the reader needs.

Then, if they want more

A practical test is whether someone reading a diff without an IDE can tell the type from the line.

Java already forces explicit types on fields, parameters and return types, and I like that boundary: the contract between pieces of code stays explicit, and inside methods, where the context is small, we allow var, diamond and inferred lambda parameters when the type is obvious, like a constructor call or a well-named factory.

I would avoid var where the type comes from a vague method name, and I would warn about traps like var with an empty diamond, which gives ArrayList of Object. To keep this consistent across hundreds of engineers, I would not rely on a principle alone.

I would write one short page with good and bad examples from our own code, share an IDE inspection profile and lint rules for the cases that can be checked automatically, and let reviewers link to the guide instead of arguing taste.

A small group owns the guide and reviews it when we adopt a new LTS release, so teams do not each invent their own answer for new features. And I would judge the rule by outcomes, such as review comments about unclear types and onboarding feedback, and adjust it if those get worse.

About this answer

Last updated
12 November 2026
Versions it names
Java 10, Java 8, Java 11, Java 25
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 "Ten years from now your codebase will have been written across Java 8 to Java 25 and...": https://scrivio-interview-library.netlify.app/java/types-and-values/2964f31aea/