简体   繁体   中英

invoke cplex variable from an other function in java

How can we invoke cplex (IloCplex cplexx = new IloCplex();) variable from an other function? because i need to use it frequantly from other functions but i cant do it because it behaves like a local variable.

Make it a global variable?

As such:

public class MyClass{
    private IloCplex cplexx;

    public MyClass(){
        cplexx = new IloCplex();
    }

    //use cplexx anywhere in MyClass with this.cplexx
}

If you want to use the same Cplex make a getter by adding this method to MyClass:

public IloCplex getCplexx(){
    return this.cplexx;
}

And in other classes you can go like this:

public class Main{
    public static void main(String args[]){
        MyClass mine = new MyClass();
        mine.getCplexx().solve(); //or some other logic
        System.out.println(mine.getCplexx().getObjValue());
    }
}

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM