Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

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

SomeComponent{};

public static someStaticMethod{};
}
@Service
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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading