skip to Main Content

I have a pretty simple project and the specific line in question is the following:

    static final Locale defaultLocale = Locale.of("en", "US");

And vscode gives the following code

The method of(String, String) is undefined for the type LocaleJava(67108964)

enter image description here

Its worth mentioning that i can compile this code with gradle, now i thought maybe its a cache thing so i decided to switch to maven instead and i got the same error.

Any ideas? this error is driving me crazy.

Other editors like Intellij dont give this error

3

Answers


  1. Probably you have some additional libraries or custom configurations in Gradle and IntelliJ that are not picked up by VS Code. Try

    import java.util.Optional;
    

    or using Locale.Builder:

    static final Locale defaultLocale = new Locale.Builder().setLanguage("en").setRegion("US").build();
    
    Login or Signup to reply.
  2. Java 19+

    The convenient static factory methods Locale.of were added to Java 19. You appear to be compiling for an earlier version of Java.


    By the way, if using Visual Studio Code, consider: Introducing the Oracle Java Platform Extension for Visual Studio Code

    Login or Signup to reply.
  3. this method was added in Java19. So if your Java version is lower than it, it may not work. You can do this by installing the new version. Or use the constructor to see if the problem can be solved. It’s like thisLocale locale=new Locale("en","US");

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search