SonarLint, Array of String: Declare this local variable with "var" instead = error
Asked Answered
U

1

5

With Java 11, for this code:

String[] arrayString = {"foo", "bar"};

SonarLint say Declare this local variable with "var" instead.

So, I have tried:

var arrayString = {"foo", "bar"};
// or
var[] arrayString = {"foo", "bar"};

But now I get these errors:

  • Array initializer needs an explicit target-type
  • 'var' is not allowed as an element type of an array

How can I declare correctly array variables or attributes.

Underact answered 10/5, 2021 at 12:58 Comment(0)
P
9

You could use

var arrayString = new String[]{"foo", "bar"};
Pollux answered 10/5, 2021 at 13:1 Comment(1)
Lol. "Use var so you don't have to declare the type!", they said.Territorial

© 2022 - 2025 — McMap. All rights reserved.