1

I have a static block which fetches data from database. This data is then being used by instance method. Since static block will be loaded once I fear that the data won't change in its lifetime.

How can I load the class (static block) when a new instance is being created?

4

Code which should be executed for every new instance logically belongs in the class' constructor. I'd suggest you move the data-fetching code to the constructor, instead of messing around with what would be more complex solutions to unload and reload the class itself in order to execute your static code.

If you don't think that DB access should occur inside of the constructor (a valid viewpoint in some circumstances) then you could consider a factory pattern to create instances of this class, or have the constructor take an instance of something which implements some kind of DataPopulator interface so that you are at least segregating the DB code from whatever it is this class actually does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.