简体   繁体   中英

Getter method is returning 0 value

I'm creating a class MDA_EFSM and it has two variable int k and int[] listA and creating setter and getter methods to initialize these two variables. Then I'm calling getter method of MDA_EFSM in another class. The getter method should return recently set value, but it is returning '0'.

public class MDA_EFSM {
    int k;
    public int listA[] = {0, 1};   

    public int getK() {
        return k;
    }
    public void setK(int k) {
        this.k = k;
    }

    public int[] getA() {
        return listA;
    }
}

public class State {
    MDA_EFSM mda = new MDA_EFSM();

    public void setMda(MDA_EFSM mdaefsm)
    {
        mda = mdaefsm;
    }
    public MDA_EFSM getMda() {
        return mda;
    }
}

public class S0 extends State{

    public void Insert_cups(int n){
        if (n > 0){
            int value = mda.getK();
        }
    }
}

I am setting value in one class and getting that value from another class. Here is the code snippet of that class:

public class S1 extends State{
    public void Insert(int n){
        if (n > 0){
            mda.setK(n);
        }
    }
}

I expect the output of recently set value, but getter method is returning '0'

You haven't set any value. You got default value of int. By the way I cannot see in code you set any value for int.

Each of your class S0 and S1 has an own instance of MDA_EFSM (by the way you should read Java naming convention). You set the Value of k in S1 but read the value of another k in S0. To achieve what you want k hast to bee static.

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