3

can you change that to satisfy sonarLint? if I do not do the assignment the value of my variable is null ...

@SpringBootApplication
public class BwsApplication {
    private static ConfigClass config;

    public BwsApplication(ConfigClass configClass) {
        config = configClass;//SONAR - Remove this assignment of "config"
    }

    public static void main(String[] args) throws SQLException {
        SpringApplication.run(BwsApplication.class, args);
        Connection con = config.getConnection();
        int number = StudentsManager.getStudentsNumber(con);
        QuartzApp qa = new QuartzApp(config);
        qa.excecution(number );
    }
}

"Static fields should not be updated in constructors" I need to use the variable in a static context!

sanastasiadis
  • 1,182
  • 1
  • 15
  • 23
Manzini
  • 117
  • 1
  • 10
  • Instead of making the assignment in the constructor, you could declare a `public static void init(ConfigClass configClass) { config = configClass; }`. If you know that only a single instance of `BwsApplication` will exist, make it a [Singleton](https://en.wikipedia.org/wiki/Singleton_pattern). – Turing85 Jun 29 '18 at 12:07
  • how does this make any sense? do you understand why updating a static field in a constructor makes no sense? what do you mean, you need to use the variable in a static context, and why do you need to change it for each instance (also: should existing instances get the same value as the one passed to the new instance))? – Stultuske Jun 29 '18 at 12:08

2 Answers2

3

The class is annotated with @SpringBootApplication.
And for the record, the following is the constructor that is called by Spring Boot during the creation of the bean application :

public BwsApplication(ConfigClass configClass) {
    config = configClass;//SONAR - Remove this assignment of "config"
}

The SpringBootApplication represented by this class will be instantiated one and only one time. So from a logical point of view, it seems not invalid to make this field a static field. But in the facts, making it static or no doesn't matter as it will be shared with no more than one instance. You spare nothing by making it static.
I think you made the field static because you would use it in the static main method.
But it is the not the way to do that.
Instead, remove the static modifier and move this code in a instance @PostConstruct method that will be executed after the dependency injection was performed on this bean :

Connection con = config.getConnection();
int number = StudentsManager.getStudentsNumber(con);
QuartzApp qa = new QuartzApp(config);
qa.excecution(number );

In this way, you could refer the config field even it is an instance field.

It could look like :

@SpringBootApplication
public class BwsApplication {

    private  ConfigClass config;

    public BwsApplication(ConfigClass configClass) {
        config = configClass;//SONAR - Remove this assignment of "config"
    }

    public static void main(String[] args) throws SQLException {
        SpringApplication.run(BwsApplication.class, args);       
    }

    @PostConstruct
    private void postConstruct() {
      Connection con = config.getConnection();
      int number = StudentsManager.getStudentsNumber(con);
      QuartzApp qa = new QuartzApp(config);
      qa.excecution(number);
    }
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
1

This code looks odd and SonarLint is right to report a code smell. Your field config is declared as static, i.e. it is shared by all instances of BwsApplication. Demanding a new ConfigClass instance by each constructor call and overriding the static fields is most probably not what you want.

You can fix this by making the assignment in some static method, e.g.:

public class BwsApplication {
  private static ConfigClass config;
  ...
  public static final void init(final ConfigClass configClass) {
    config = configClass;
  }
}

Your constructor will no longer need an argument of type ConfigClass.


If you can determine the instance of config at compile-time, you can deply JohnSnowDoesNotKnowNothing's solution and deploy a static initializer block.


Just to rule out the possibility: Please make sure you did not confuse static with final.

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • In general yes but it is a spring boot application class. The `static` modifier was a workaround to interact with the field in the `main()` method. You can refer to my answer to understand. – davidxxx Jun 29 '18 at 12:32