Nested class AsyncTask can't modify Outer Class static objects

Tom

I have a problem with Nested java class, it sees outer class object but somehow fails to modify it. I read a lot of similar questions but couldn't find solution for this one. It might be something really simple but I'm not good enough to figure it out. I have Sorter class to do some calculations in the background, I decided to use AsyncTask to perform those calculations outside of UI Thread. My class looks like this

public class Sorter
{
    private static List<Long> workingList;
    private static int _numberOfContainers, _containerSize, _timesToRepeat;
    private static Long _numbersFrom, _numbersTo, _sortingAlgorithmId;

    public Sorter(int numberOfContainers, int containerSize, Long numbersFrom, Long numbersTo,
                  int timesToRepeat, Long sortingAlgorithmId)
    {
        _numberOfContainers = numberOfContainers;
        _containerSize = containerSize;
        _numbersFrom = numbersFrom;
        _numbersTo = numbersTo;
        _timesToRepeat = timesToRepeat;
        _sortingAlgorithmId = sortingAlgorithmId;
        // perform calculations in the background
        new BackgroundCalculations().execute();
    }

    static class BackgroundCalculations extends AsyncTask<Void,Void,Void>
    {

        @Override
        protected Void doInBackground(Void... voids)
        {
            workingList = new ArrayList<>(_containerSize);
            // workingList is still null after this
            _numbersTo += 1; // to fix exclusive number range to inclusive
            Random rand = new Random();
            for (int i = 0; i < _containerSize; i++)
            {
                workingList.add((long) (rand.nextDouble() * (_numbersTo - _numbersFrom)) + _numbersFrom))
            }
            // some calc
            return null;
        }
    }


}

I tried to instantiate workingList in Sorter constructor but nested class fails to add items to workingList anyway. Any solutions? Maybe better way to implement background calculations without such problems?

GhostCat

You are mixing up two concepts here.

Your methods are all static; and your fields are two. So why do you then use a constructor; indicating that you want an object of your Sorter class to be instantiated?

So, the first thing to fix your problem - get a better understanding of the concepts you are using.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Static nested class has full access to private outer class members?

From Dev

Access outer class in inner nested class

From Dev

Can't access outer class from fragment class android

From Dev

Static nested class has access to private constructor of outer class

From Dev

Working of nested class objects?

From Dev

Can a static nested class be used as jsp bean?

From Dev

Can GSON handle static private nested class

From Dev

Inner class access to outer class nested enum

From Dev

Referring to static nested class object from outer class

From Dev

Why can't a class extend a static nested class occurring within it?

From Dev

Can't reference inner class from a static context, but only if outer class is generic

From Dev

Can't access outer class from anonymous class

From Dev

Why an Outer Class Can’t Be Static?

From Dev

Why can’t this static inner class call a non-static method on its outer class?

From Dev

Access outer class in inner nested class

From Dev

Can't access outer class from fragment class android

From Dev

Static nested class has access to private constructor of outer class

From Dev

Can a static nested class be used as jsp bean?

From Dev

Can't call static method inside class

From Dev

Inner class access to outer class nested enum

From Dev

Referring to static nested class object from outer class

From Dev

Static objects of nested class can be declared without class definition

From Dev

C++ can't modify private variable with setter in a class of class

From Dev

Java: reference outer class in nested static class

From Dev

How does static inner class can access all the static data members and static member function of outer class?

From Dev

In C++, how can I use a nested class type in an other nested class (the two nested classes are under the same outer class)

From Dev

Android class with nested asynctask

From Dev

Modify array with nested class

From Dev

Can audio be implemented in a static nested class?

Related Related

  1. 1

    Static nested class has full access to private outer class members?

  2. 2

    Access outer class in inner nested class

  3. 3

    Can't access outer class from fragment class android

  4. 4

    Static nested class has access to private constructor of outer class

  5. 5

    Working of nested class objects?

  6. 6

    Can a static nested class be used as jsp bean?

  7. 7

    Can GSON handle static private nested class

  8. 8

    Inner class access to outer class nested enum

  9. 9

    Referring to static nested class object from outer class

  10. 10

    Why can't a class extend a static nested class occurring within it?

  11. 11

    Can't reference inner class from a static context, but only if outer class is generic

  12. 12

    Can't access outer class from anonymous class

  13. 13

    Why an Outer Class Can’t Be Static?

  14. 14

    Why can’t this static inner class call a non-static method on its outer class?

  15. 15

    Access outer class in inner nested class

  16. 16

    Can't access outer class from fragment class android

  17. 17

    Static nested class has access to private constructor of outer class

  18. 18

    Can a static nested class be used as jsp bean?

  19. 19

    Can't call static method inside class

  20. 20

    Inner class access to outer class nested enum

  21. 21

    Referring to static nested class object from outer class

  22. 22

    Static objects of nested class can be declared without class definition

  23. 23

    C++ can't modify private variable with setter in a class of class

  24. 24

    Java: reference outer class in nested static class

  25. 25

    How does static inner class can access all the static data members and static member function of outer class?

  26. 26

    In C++, how can I use a nested class type in an other nested class (the two nested classes are under the same outer class)

  27. 27

    Android class with nested asynctask

  28. 28

    Modify array with nested class

  29. 29

    Can audio be implemented in a static nested class?

HotTag

Archive