简体   繁体   中英

How to access the local variables outside the method in same class in java

I am doing coding in android studio. I want to access the variable totalCuisine++ outside this method. Also totalCuisines is declared globally in claass ie 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();
    }
}

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;

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