Expand my Community achievements bar.

Refactor this code to use a "static final" pattern.

Avatar

Level 5

I've written an API for the navigation and the below line of code is throwing a sonarlint error "Refactor this code to use a 'static final' pattern".

 

goyalkritika_0-1683619748703.png

 

Can someone tell me what is the issue if we replace the special characters like above? And how to resolve it?

 

3 Replies

Avatar

Community Advisor

Hello @goyalkritika ,

  • A Pattern is compiled from a String literal or constant and is not stored in a static final reference.

  • String.matches, String.split, String.replaceAll or String.replaceFirst are invoked with a String literal or constant. In this case, the code should be refactored to use a java.util.regex.Pattern while respecting the previous rule.

Here is a recommended way to write your code. Follow this ticket-> https://sonarsource.atlassian.net/browse/RSPEC-4248  

Avatar

Level 5

@Sady_Rifat I tried making the 'str' variable private static, but it is not accepting it. 

 

Can you think of any other solution to resolve this issue?