Refactor this code to use a "static final" pattern. | Community
Skip to main content
May 9, 2023

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

  • May 9, 2023
  • 2 replies
  • 3389 views

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

 

 

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

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

Sady_Rifat
Community Advisor
Community Advisor
May 9, 2023

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  

May 11, 2023

@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?

Rohit_Utreja
Community Advisor
Community Advisor
May 9, 2023