I'm new to Java and I'm constructing a springboot application where different classes require data from the same source. The source is a couple of files, but the important thing is that it takes quite some time to get the data out; getting all the data out is about as fast as getting each piece that the different classes require out, so partitioning the function doesn't help.
Therefore I'd like to have a class that is initialized once, gets all the data, and then serves the classes that require the data. Ideally it would be initialized only if requested, and the data would then be saved in the instance.
Let's say I have the class:
package myapp; import java.util.concurrent.TimeUnit; import java.lang.InterruptedException; public class ExampleClass { private int usefulValue; public ExampleClass(){ this.usefulValue = slowMethod(); } private int slowMethod(){ //just an example of something that takes time int usefulValue; try { TimeUnit.SECONDS.sleep(500); } catch (InterruptedException e){ ; } usefulValue = 15; return usefulValue; } public int getUsefulValue(){ return this.usefulValue; } } How can I get that to run and usefulValue to be available to the other classes in the package without reloading it in each separate class?
The values are extremely manageable memory-wise and I'm looking specifically for an in-memory solution; I could just write it to a file/db or have a socket-server running that serves the application but the question is relating to what's possible to do in Java.
Ps. usefulValue changes about once per day