IntelliJ IDEA 2017.3 EAP brings support for Local-Variable Type Inference

Posted on by Zlata Kalyuzhnaya

IntelliJ IDEA 2017.3 EAP brings support for Local-Variable Type Inference.

A long-awaited Java 9 has been released just a few weeks ago, but we’re already looking forward to the new goodies Java 10 is going to bring. Though the chances are it might not be called Java 10. Oracle has proposed moving Java to releases every six months, so we might have the next release already as early as March 2018, and will probably be named Java 18.3. We’ll see.

The upcoming release of Java is going to bring Local-Variable Type Inference according to the JEP 286. This specification aims to reduce the boilerplate code while declaring local variables.

IntelliJ IDEA 2017.3 supports Local-Variable Type Inference. The IDE will include a prompt to replace the explicit types with ‘var’, when the type can be inferred from the context.

inspection-LVTI
Currently the IDE detects local variables with ‘var‘ types when they can be replaced with explicit types.

var-explicittype

This new feature allows you to sneak-a-peek into the future Java release, so we hope you’ll give it a try. As this is an experimental feature, we count on your feedback a lot. But please keep in mind that the JEP 286 has not been released yet so it might still change, and if so, the support for this feature in our IDE will be changed accordingly. Do share your thoughts with us here in the comments, on twitter or in our issue tracker.

Happy developing!

 

 

Comments below can no longer be edited.

18 Responses to IntelliJ IDEA 2017.3 EAP brings support for Local-Variable Type Inference

  1. Josh says:

    November 2, 2017

    As a C# dev turned Java dev, why not do something that would work before Java 10 gets a release? Hide the variable declaration when it’s obvious from context so that we get to see this feature today (but still save the correct type to the file). A few of the long time Java devs that I work with are unconvinced of the need for the var functionality in the language. This would go a long way to showing them that it works well in practice and reduces the noise when reading code.

    • David Zemon says:

      November 4, 2017

      I recently found out about the “advanced code folding” plugin which does exactly this. You should check it out 🙂

    • Florian Tavares says:

      November 8, 2017

      If you have stuck with Java, use Lombok. There is good IDE support and support for var/val, data classes and much more

      • Catlin Cox says:

        March 5, 2018

        Unfortunately though, Lombok has some issues with working with Java 9 and above. Unless they have fixed this issue, but the last work around I saw was adding endless compiler arguments, which are annoying.

        • Bernhard says:

          August 27, 2018

          All this issues are fixed, I am using java 10 and lombok 1.18.2. It also adds new Features, like FieldNameConstants and SuperBuilder (Builder with inheritance).

  2. Brice says:

    November 3, 2017

    404 not found on the screenshot

    • Zlata Kalyuzhnaya says:

      November 3, 2017

      Brice, thank you. its fixed now.

      • soroosh says:

        November 5, 2017

        the link of 2nd screen shot is not fixed yet.

        • Zlata Kalyuzhnaya says:

          November 7, 2017

          soroosh, thank you. The 2nd screenshot was updated. Now it should work.

  3. Sarah says:

    November 6, 2017

    I’m mainly a Java developer, but have a small bit of experience with C#. I hope that this does not over emphasize the use of the ‘var’ keyword like what ReSharper does in Visual Studio, or I may end up having to completely disable it.

    I’m not opposed to using it, but I know that my coworkers are going to throw a fit if they see it used liberally.

  4. Mateus says:

    November 8, 2017

    I’m trying testing this new feature but I can’t. It doesn’t work.

    • Brian says:

      November 10, 2017

      i installed the jdk10 ea, and set my project language level to “experimental features”, and it worked okay for me.

  5. Dmitry says:

    December 4, 2017

    hm, doesn’t work for me in IntelliJ 2017.3.1 EAP on macOS. I don’t see “experimental features”

    • Anna Kozlova says:

      December 4, 2017

      Create var i = 0; and see if you have a suggestion on var to switch on experimental features. Should show dialog with confirmation and further highlighting/inspections should be enabled.

      • Dmitry says:

        December 4, 2017

        Thanks, Anna. It works now. After typing var and accepting Agreement I had to restart my IntelliJ for inspections to work. Before restart, I was able to use var but inspections Java Experimental were not present in IDE. Thanks

  6. jinuo.fang says:

    January 11, 2018

    how to disable it ?

  7. Paul says:

    April 6, 2018

    I just tried this on Intellij 2018.1. Although the var keyword is supported without any error and we can use the “light bulb” to replace var with explicit types to see what types it is, there are still quite a few important usage that’s still missing.

    (1) In fact the var keyword is mostly used when the object type is some lengthy generics types, or for quick usage to chain up a few stream / lambda expressions. It would be very helpful if the mouse highlight tooltip shows the actual type behind the “var” keyword.

    (2) I found that if I use “Find Usage” on certain classes. If it is referred by a “var” variable it is not counted as “used”.

  8. Java 10 – czyli “var” na pokładzie – CodeCouple.pl says:

    December 24, 2018

    […] IntelliJ ma bardzo dobre wsparcie dla tego feature. […]

Subscribe

Subscribe for updates