Java variables overwriting

萝らか妹 提交于 2021-02-05 06:40:30

问题


I have a program with a for that take strings from an Arraylist, split them and sends them to a Worker class.

Worker Class:

public class WorkerRSR extends SwingWorker<String, Void>{
private static String urlFrames;
private static String urlImg;
public static int bool;
public static int dist;
public static int numI;
public static int spra;
public static boolean isCoda;
public static int numCoda;
public static String algo;

public WorkerRSR(String urlImg, int dist, int numI, int spra, String algo, String urlFrames, boolean isCoda, int numCoda) {
    this.urlImg=urlImg;
    this.dist=dist;
    this.numI=numI;
    this.spra=spra;
    this.algo=algo;
    this.urlFrames=urlFrames;
    this.isCoda = isCoda;
    this.numCoda = numCoda;

//FIRST CHECK POINT

}

    @Override
    protected String doInBackground() throws Exception {
        PanelRSR_LRSR.getProgessbar().setIndeterminate(true);
        go();
        return  "";
    }

    @Override
    protected void done() {
        System.out.println("Algoritmo RSR esguito");
        if(isCoda){
            CreateOption.codaCont++;
            System.out.println("RSR codaCont: "+CreateOption.codaCont);
            if(CreateOption.codaCont==CreateOption.csize){
                JOptionPane.showMessageDialog(null,"Coda Eseguita", "Attenzione",JOptionPane.WARNING_MESSAGE);
                PanelRSR_LRSR.getProgessbar().setIndeterminate(false);
            }
        }
        else{
            PanelRSR_LRSR.getProgessbar().setIndeterminate(false);
            JOptionPane.showMessageDialog(null,"Finito RSR", "Attenzione",JOptionPane.WARNING_MESSAGE);
        }
    }

    public static void go() throws IOException{
        System.out.println("ESEGUO RSR, attendi...");

//SECOND CHECK POINT

        System.out.println("RSR n = "+numI+" codaCont: "+CreateOption.codaCont+" numCoda = "+numCoda);
        while(true){
            if(numCoda==CreateOption.codaCont)
                break;
        }
        MakeRSR m=new MakeRSR();
        String name = urlImg.substring(urlImg.lastIndexOf("\\"),urlImg.lastIndexOf("."));
        String output=name.substring(1); //?
        String urlOutput=urlFrames+"\\finalRSR\\"+name+"-"+algo+"-dist"+dist+"-n"+numI+"-N"+spra+".png";
        m.RSR(urlImg,urlOutput,dist,numI,spra);
    }
}

The problem is that this class is going to be called multiple times and every time it overwrite the previous values of the varables: if I check them at the first Checkpoint they are different (probably beacuse the second acquisition yet has to be made), but at the second Checkpoint they are the same. How can I let them stay different?


回答1:


These variables shouldn't be static if they are set by the constructor. They should be instance variables, so each instance of your class can have different values.

public class WorkerRSR extends SwingWorker<String, Void>{
    private String urlFrames;
    private String urlImg;
    private int bool;
    private int dist;
    private int numI;
    private int spra;
    private boolean isCoda;
    private int numCoda;
    private String algo;

    public WorkerRSR(String urlImg, int dist, int numI, int spra, String algo, String urlFrames, boolean isCoda, int numCoda) {
        this.urlImg=urlImg;
        this.dist=dist;
        this.numI=numI;
        this.spra=spra;
        this.algo=algo;
        this.urlFrames=urlFrames;
        this.isCoda = isCoda;
        this.numCoda = numCoda;

//FIRST CHECK POINT

    }

    ...
}

You should also change all those variables to be private. If they should be accessed from outside the class, they should be accessed via getter methods.



来源:https://stackoverflow.com/questions/32455697/java-variables-overwriting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!