[Solved] jenkins checkstyle: local variable hides a field

Source code:

private static ApplicationContext applicationContext;
public static void setApplicationContextValue(ApplicationContext applicationContext){
    SpringContextUtil.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext(){
    return applicationContext;
}

The checkstyle on Jenkins prompts the setApplicationContextValue() method to “hides a field”

The error message usually appears on the setter method of the variable, the reason is:

It means you’ve got two different variables with the same name-myBoard. One of them is a field in your class. Another one is a local variable, that is, one that you’ve declared inside a method.

It’s a bad idea to have two variables with the same name. It can make your code very confusing and difficult to maintain.

This means that two variables are set with the same name, one is a class variable, the other is a local variable in the method, the solution:

public static void setApplicationContextValue(ApplicationContext applicationContext1){
    SpringContextUtil.applicationContext = applicationContext1;
}

Change the name of the internal parameter of the method to distinguish it from class variables, such as applicationContext1.

 

Similar Posts: