Uploaded image for project: 'SonarJava'
  1. SonarJava
  2. SONARJAVA-2706

Java 10 local variable declaration statement using 'var' should be handled

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 5.3
    • Component/s: Plugin, Semantic
    • Labels:
      None

      Description

      Java 10 introduce the local variable declarator var, to be used instead of explicitly declaring a type with local variable. The type of the variable is then the upward projection of the variable initializer type.

      • Java 9:
        List<String> items = new ArrayList<>();
        
      • Java 10:
        var items = new ArrayList<String>();
        

      See JLS10 - §14.4 - Local Variable Declaration Statements for details about semantic.

      Note that according to JLS10 - §3.9, and is not part of the restricted keywords introduced for modules in Java 9:

      • var is not a keyword, but rather an identifier with special meaning as the type of a local variable declaration.

      As other local variable declarations, it means that it can be used as well in declaration of for and foreach statements (see JLS10 - §14.14), as well as in declaration of a resource in a try-with-resource statement (see JLS10 - §14.20.3)

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                michael.gumowski Michael Gumowski
                Reporter:
                michael.gumowski Michael Gumowski
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: