1

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

1
  • you can define this field as "public static usefulvalue" so you can use its value every where like this "ExampleClass.usefulvalue" Commented Oct 21, 2018 at 20:07

2 Answers 2

1
package myapp; import java.util.concurrent.TimeUnit; import java.lang.InterruptedException; public class ExampleClass { public static int usefulValue; public ExampleClass(){ ExampleClass.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 ExampleClass.usefulValue; } } 

Also in any other class you can use its value like this ExampleClass.usefulValue so its value remains same through out the appilcation or in this thread.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried to implement this and having another class calling ExampleClass.usefulValue but I get 0, the value isn't updated to 15, any thoughts on what might be wrong?
Just needed to be initialized somewhere, works, thanks!
1

You can use lazy initialization.

 private Integer usefulValue; public ExampleClass(){ // this.usefulValue = slowMethod(); } public int getUsefulValue(){ if (this.usefulValue == null) { this.usefulValue = slowMethod(); } return this.usefulValue; } 

or use for this: lombok

1 Comment

Together with the other response it completely resolves my problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.