How should I structure a Java application, where do I put my classes?

Question :

How should I structure a Java application, where do I put my classes?,

Answer :

First of all, I know how to build a Java application. But I have always been puzzled about where to put my classes. There are proponents for organizing the packages in a strictly domain oriented fashion, others separate by tier.

I myself have always had problems with

  • naming,
  • placing

So,

  1. Where do you put your domain specific constants (and what is the best name for such a class)?
  2. Where do you put classes for stuff which is both infrastructural and domain specific (for instance I have a FileStorageStrategy class, which stores the files either in the database, or alternatively in database)?
  3. Where to put Exceptions?
  4. Are there any standards to which I can refer?
Read More  Generic type checking

,

I’ve really come to like Maven’s

One of the key ideas for me is to have two source roots – one for production code and one for test code like so:

MyProject/src/main/java/com/acme/Widget.java  MyProject/src/test/java/com/acme/WidgetTest.java  

(here, both src/main/java and src/test/java are source roots).

Advantages:

  • Your tests have package (or “default”) level access to your classes under test.
  • You can easily package only your production sources into a JAR by dropping src/test/java as a source root.

One rule of thumb about class placement and packages:

Read More  How should I handle a situation where I need to store several unrelated types but provide specific types on demand?

Generally speaking, well structured projects will be free of  Learn when they are bad (and when they are not), and consider a tool like or  that will help you eliminate them.

That’s the answer How should I structure a Java application, where do I put my classes?, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you

Read More  Why doesn’t inheritance work the way I think it should work?