The one class per file rule in Java has me a bit confused. I writing an Android app and trying to implement the accepted answer to this question:
Common class for AsyncTask in Android?
which calls for an interface definition which class A implements and class B accepts as an argument to its constructor.
So I need an A.java and a B.java, but where does the interface go? Does it need a separate java file itself? Do I have to define it inside both A and B? If not how to import it?
Also I will have about 10 different AsyncTask classes, but I don’t want to bother creating a new file for each one. What would you recommend? Is there a way to put all 10 classes in one file? Or should I create a big if/then block inside the class and pass an argument telling it which of the 10 different tasks I want it to do?
You have to place it in
AsyncTaskCompleteListener.java. If it is in the same package, then there is no need to import it. If in a different package, you can import it using theimportstatement.I’d suggest reading a java tutorial before going further.
As for the 10+ classes – you can use
public static classinside another class. This would work, but having a file for each class is something you should get used to – it is the preferred option. The inner static class is used only if a logical relating of the inner classes exists to their owning class.