I'm trying to make an application ported to Java threadsafe and as such am looking to minimise the amount of static variables required.
I will show an example of an issue I want clarity on. I have a class Eparams
as follows:
public class Eparams {
private double a;
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
}
I set the value in another class, Epi
.
public class Epi {
static Eparams eparams = new Eparams();
public void epi() {
eparams.setA(3.44);
}
public Eparams getEparams() {
return eparams;
}
}
I want to access the value of a
of type Eparams
in another class, EpiParts
:
public class EpiParts {
public void test() {
Epi epi = new Epi();
Eparams eparams = epi.getEparams();
double val= eparams.getA();
System.out.print(val);
}
}
I need Eparams
values to be non-static and there are multiple threads accessing this class. Is the way I've done it the best way to achieve this?
If I declare in Epi
a new instance of Eparams
to be static, what are the implications of threads accessing this instance? Making this instance static was the only way I got it to work.
Is this the wrong way to go about this? Is there an easier way to retrieve values across different classes in a threadsafe manner (apart from function arguments and return values)?