0

Using static-method makes code more clean. So I wanna use static-method even used with @Service, @Repository class in it. You can more easily understand by code. Very short one and It works!!

But I want to know it is okay to use in any situation. I didn't see like that code before, so I am afraid it is the effective code to use. If you have any idea about that, could you advise me, please?

@Repository
public class TruckRepository {
     public Integer selectWheelCount() {
          //which is searching truck database to get some data about trucks.
          //Such as how many wheels does the truck have, something like that.
     }
}


@Component
public class CarFactory {
     private static TruckRepository truckRepository;

     //@Autowired << can be omitted after spring 4.3 as I know
     NewsSourceFactory(TruckRepository truckRepository) {
          this.truckRepository = truckRepository;
     } 

     public static Integer getWheelCount(String carType) {
          swtich(carType) {
               case TRUCK:
                   return truckRepository.selectWheelCount();
          }
     }

}



@Component
public class SomeCode {
     public void something() {
          Integer count = CarFactory.getWheelCount("TRUCK");
     }

}




Add Comments

I very empressive the code of "Duration.class", "Stream.class" in java. They are also using static-method, Of course they have no dynamic injection in there. Just in case of thinking about the concise of code or clearness, isn't it the merit of static-method, dont you think? is it really harmless?

  • 4
    Don't... Please don't... Now you have created something that is hard to maintain (you cannot easily test your `SomeCode` class anymore due to static methods). – M. Deinum Aug 06 '19 at 07:49
  • [*When to use static methods*](https://stackoverflow.com/questions/2671496/java-when-to-use-static-methods/20522548) – BackSlash Aug 06 '19 at 07:55
  • 1
    why you think is "more clean" ? – jpganz18 Aug 06 '19 at 07:56
  • I saw in a domain driven design https://en.wikipedia.org/wiki/Domain-driven_design multiple services and other classes static, since the architecture is more based on domains ... you might take a look, anyways I dont think it will be fully static specially if you are using spring and you will want to have dependency injections or other annotations – jpganz18 Aug 06 '19 at 07:59
  • After stream comes to java, I think Stream.of() or Collectors.toList() something like that codes makes more comfortable to make some codes. It can be hide the complexity of codes to get some output. Such as When do we want to change map to list, we have to write about 3 lines to make it before. But now we can just use .stream() and collect() to change map to list. That is also static-method but using that make code more clean than before. @BackSlash thank you for adding reference. I will read it. – user10252289 Aug 06 '19 at 08:02

1 Answers1

0

You are using static method which uses static field which is initialized in constructor. In this code it's not even clear when Spring will create a new instance of CarFactory (maybe it won't at all, if none is referencing it). And if no instance of CarFactory created, your static method is broken too, because static field is not initialized.

I don't see any benefits of using static methods in your case, after all you can always inject instance of CarFactory into SomeCode.

Simple as:

@Autowired
private CarFactory cartFactory;

Or better:

private CarFactory cartFactory;
public SomeCode(@Autowired CarFactory pCartFactory) {
    cartFactory = pCartFactory;
}
Akirus
  • 108
  • 2
  • 11