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

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!

 

 

About Zlata Kalyuzhnaya

IntelliJ IDEA Marketing Manager at JetBrains.
This entry was posted in EAP Releases, New Features and tagged , , , , . Bookmark the permalink.

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

  1. Josh says:

    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:

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

    • Florian Tavares says:

      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:

        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.

  2. Brice says:

    404 not found on the screenshot

  3. Sarah says:

    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:

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

  5. Dmitry says:

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

    • Anna Kozlova says:

      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:

        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:

    how to disable it ?

  7. Paul says:

    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”.

Leave a Reply

Your email address will not be published. Required fields are marked *