3

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?

Community
  • 1
  • 1

2 Answers2

2

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 the import statement.

I'd suggest reading a java tutorial before going further.

As for the 10+ classes - you can use public static class inside 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.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • I created the 8 different files I need and am already regretting it as I already have to make changes across all 8 files in the areas common to each class. And there will be more when I get to logging and testing and if I change the way I document.. Anyone have a link for a good example of using 'public static class' and keeping your common classes together in one file? –  Dec 25 '10 at 21:47
  • Are you editing your code in an IDE like Eclipse, NetBeans or IntelliJ? If so, then tasks like renaming etc will update all of the files that reference the name. I strongly recommend using an IDE versus a simple text editor. – Neil Bartlett Dec 25 '10 at 22:05
  • I'm using Eclipse in VI mode and I'm extremely efficient at working with code in one file. Thanks for the rename tip, but in this case it wasn't renaming. Forcing me to use my mouse to open 8 different files to make 8 simple copy and pastes is ridiculous. –  Dec 25 '10 at 22:30
  • If you are copy-and-pasting code, then you are doing something wrong. Factor the repeated code into separate classes or methods. – Skip Head Dec 25 '10 at 23:35
  • Its not me that requires the separate but similar classes its Android and Java. But even if I could - factoring such simple classes into even more classes just adds to your lines of code and increases the maintenance burden. –  Dec 26 '10 at 03:13
2

where does the interface go? Does it need a separate java file itself?

Yes. You could make the interface and/or one of the classes non-public, but then you could not use them outside the package.

Also I will have about 10 different AsyncTask classes, but I don't want to bother creating a new file for each one.

Why not? Having one class or interface per file is how it's generally done in Java. I suggest accepting this convention, as trying to go against it will cause you an endless stream of problems.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720