I am relatively new (ish) to Java..
I am writing an Android app, and now I am going back over my code and tidying up and adhering my coding structure to a more best practice style.
I am building methods and classes as I see fit to avoid the numerous amounts of duplicate code that I have produced. I have found myself trying to create a class (e.g. HeavyStuff.java) that contains several AsyncTask methods inside it (e.g. MyTask1 and MyTask2). When calling the class from an activity, I'd like to execute MyTask1 at some point, and at some point elsewhere I'd like to execute MyTask2. I am trying to use the following respectively:
HeavyStuff.MyTask1 myTask1 = new HeavyStuff.MyTask1();
myTask1.execute();
And
HeavyStuff.MyTask2 myTask2 = new HeavyStuff.MyTask2();
myTask2.execute();
The problem is, I get an error saying that "HeavyTest is not an enclosing class". My class looks like this:
package com.wizzkidd.myapp;
import android.os.AsyncTask;
public class HeavyStuff {
public class MyTask1 extends AsyncTask<String, String, String> {
//...
//...
}
public class MyTask2 extends AsyncTask<String, String, String> {
//...
//...
}
}
The class can also be seen here in full: http://hastebin.com/yahihokupu
What am I missing that is required to make the class an "enclosing class".
-----EDIT-----
I've looked at the answer that was given as a possible duplicate to my question and it does not work. The answer given recommends using static
for my inner class, but this doesn't work for me.
I have however found that I can do this:
HeavyStuff.MyTask1 myTask1 = new HeavyStuff().new MyTask1();
myTask1.execute();
It works, but what are the implications (if any) when doing it like this? Is it bad practice?