-2

I am doing coding in android studio. I want to access the variable totalCuisine++ outside this method. Also totalCuisines is declared globally in claass i.e int totalCuisines = 0;

It is a part of big program but main problem is here in front of u, rest of all programs are executing successfully

public class CuisinesFilterDialog extends DialogFragment{

    int totalCuisines = 0;

    ArrayList<CuisinesModel> data = new ArrayList<>();

{
  private void updateFilterStats() {

        ArrayList allData = new ArrayList();
        if(data != null && data.size() > 0){
            for(int i=0; i<data.size(); i++){
                if(data.get(i).isSelected()) {
                    allData.add(data.get(i).getName());
                    totalCuisines++;// I want to access it outside this method
                }
            }
            DataStore.putString(getContext(), ZConstants.SortFilter.KEY_EXTRAS_FILTER_CUISINES,
                    TextUtils.join(",",allData));
        }else{
            DataStore.putString(getContext(), ZConstants.SortFilter.KEY_EXTRAS_FILTER_CUISINES,
                    ZConstants.EMPTY_STRING);
        }
        dismiss();
    }
}
sstan
  • 35,425
  • 6
  • 48
  • 66
sriRohit
  • 27
  • 3
  • totalCuisines++ is not a variable. totalCuisines is the private class variable and ++ is the increment by 1 operation. To access this from outside the class you can either make totalCuisines public (public int totalCuisines = 0) or create a public function incrementTotalCuisines() which will increment the private int. Note that totalCuisines is accessable outside this method, other class methods can modify this variable. It's not accessable from outside this class. – JohannisK May 27 '16 at 13:09

1 Answers1

1

totalCuisines++ is not a variable. It means totalCuisines+1. However, You can access totalCuisines by several ways. The easiest way to declare totalCuisines as public variable. Like,

public int totalCuisines = 0;
Adnan
  • 8,468
  • 9
  • 29
  • 45
  • There is no modifier set meaning it shouldn't be necessary to make 'public' as long as it needs to be accessed only from the Class or Package? ref: http://stackoverflow.com/a/215505/4252352 – Mark May 27 '16 at 13:20