You can access a static
variable declared in another class using the syntax OtherClass.p15
. If the variable is an instance variable (no static
keyword in the declaration), then you need an instance of the other class to access the variable:
OtherClass thing = . . .;
. . . cplex.getValues(thing.p15[i][j][q]) . . .
In both cases, you will need to ensure that the variable has the right access qualifiers so that the referencing code can access it.
Without knowing more about your overall software structure, it's hard to say what the "most elegant" way would be to address the problem.
EDIT: After your edit, I think I see the problem. The local variable p15
ceases to exist (goes out of scope) when the method buildModel
returns. You need to store the array somewhere that stays around. The simplest change, I think, would be to modify your MainTSP
class to look like this:
public class MainTSP {
static IloNumVar[][][] p15;
static void buildModel(IloMPModeler model, Data data, IloNumVar[][][] w) throws IloException{
p15 = new IloIntVar[n][n][n];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
for(int q=0; q<n; q++){
p15[i][j][q] = model.intVar(lb, ub);
}
}
}
[...]
Then in your main
method of class Ausführung
, you can access it like
. . .
if(cplex.getValue(MainTSP.p15[i][j][q]) >= 1) . . .
There doesn't seem to be any reason to make p15
an instance field. (If for some other reason you needed it to be an instance field, you would then need to create an instance of MainTSP
using something like MainTSP tsp = new MainTSP();
.)
An alternative would be to make buildModel
return p15
and then you could assign the return value to another variable named p15
in your Ausführung
class:
public class MainTSP {
static IloNumVar[][][] buildModel(IloMPModeler model, Data data, IloNumVar[][][] w) throws IloException{
p15 = new IloIntVar[n][n][n];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
for(int q=0; q<n; q++){
p15[i][j][q] = model.intVar(lb, ub);
}
}
}
return p15;
[...]
Then:
IloNumVar[][][] p15 = MainTSP.buildModel(cplex, data, w);
(Perhaps that's what you planned to do with the w
parameter? Perhaps simply replace p15
with w
in buildModel
will solve your problem.)