0

To use a member variable in static function I need to declare that member variable as static. Is there anything wrong in declaring static fields and injecting them via constructor injection in multithreaded environment ?

@Named
public class QueryHelper {
  // Anything wrong to declare it static and injecting it via constructor injection ?
   private static QueryUtil queryUtil;

    @Inject
    QueryHelper(final QueryUtil queryUtil) {
        this.queryUtil = queryUtil;
    }

    public static List<Contracts> getContracts() {
       
      // .. some code

       queryUtil.someFunction();


      // ..some code
      return contractList();
    }
}
  • Any (good) reason against making the "static function" to an "instance one"(non-static)? (U can use static "things" .."anywhere":) – xerx593 Sep 12 '22 at 09:17
  • 2
    Generally that is an anti-pattern and should be avoided. – M. Deinum Sep 12 '22 at 09:31
  • I closed the question as a duplicate and added a few relevant questions to it. The answers/comments on these questions explain why you shouldn't use them. – g00glen00b Sep 12 '22 at 09:43

0 Answers0