Is @PostConstruct method called for a static method?


I am wondering whether @PostConstruct method is ever called when a static method from a component is called for e.g.

public class SomeComponent{
void init(){ // set up static variables};


public static someStaticMethod{};
public class SomeService{

public method(){
SomeComponent.someStaticMethod(); //is @PostConstructor called here?

Is @PostConstructor method ever called when the static method of the component is called above? If not, in what way can the @PostConstructor method be called? Much thanks!

>Solution :

No, @PostConstruct can not be applied to a static method. The whole point of this annotation is to be called on a method of a Bean after the bean has been constructed. In the case of a static method, there is no corresponding Bean (Java object) and so it doesn’t make sense to have a static @PostConstruct method.

Leave a Reply Cancel reply