Suppose I have a method with a listener (for example, I chose a request to VK Api, it was at hand):
private void getSubscribersInGroup() throws Exception { //Устанавливается пауза, чтобы код не выполнялся, пока //не будет получен ответ в слушатель isPause = true; //Создаю запрос (указываю что мне нужно получить) final VKRequest request = VKApi.groups().getMembers(VKParameters.from( VKApiConst.GROUP_ID, groupId, VKApiConst.COUNT, 0)); //Посылаю запрос и устанавливаю слушатель с методами request.executeWithListener(new VKRequest.VKRequestListener() { @Override public void onComplete(VKResponse response) { //Обрабатываю ответ и получаю его в ГЛОБАЛЬНУЮ переменную subscribersCount = getCountFromJSON(response.responseString); //Отключаю паузу, чтобы код продолжился isPause = false; } @Override public void onError(VKError e) { //По скольку это метод из API, я не могу выбрасывать Exception, //приходится прибегать к такому варианту isError = true; error = "error when requesting count: " + e.errorMessage; isPause = false; } @Override public void attemptFailed(VKRequest request, int attemptNumber, int totalAttempts) { //Аналогично предыдущему методу isError = true; error = "onAttemptFailed when requesting count: " + "attemptNumber = " + attemptNumber + "totalAttempts = " + totalAttempts; isPause = false; } }); //Метод просто вызывает в бесконечном цикле "wait" на 100 миллисекунд, //с условием (isPause) waitResponseFromServer(); //Если была ошибка — кидаю исключение if (isError) throw new Exception(error); } I want to return the variable count through return , and not to use a global variable. Listener methods have access only to final method variables, so I cannot access them directly. Android Studio offered the option to make the variable count array - final int[] count = new int[1]; and then work with the first element of the array. As for me, this is a bit of an obvious move that I don’t like much. Are there other options to return the subscribersCount variable from the method so as not to use a global variable?