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

后端 未结 10 1482
轻奢々
轻奢々 2020-12-24 05:17

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 str

10条回答
  •  臣服心动
    2020-12-24 06:17

    I'm a huge fan of organized sources, so I always create the following directory structure:

    /src - for your packages & classes
    /test - for unit tests
    /docs - for documentation, generated and manually edited
    /lib - 3rd party libraries
    /etc - unrelated stuff
    /bin (or /classes) - compiled classes, output of your compile
    /dist - for distribution packages, hopefully auto generated by a build system
    

    In /src I'm using the default Java patterns: Package names starting with your domain (org.yourdomain.yourprojectname) and class names reflecting the OOP aspect you're creating with the class (see the other commenters). Common package names like util, model, view, events are useful, too.

    I tend to put constants for a specific topic in an own class, like SessionConstants or ServiceConstants in the same package of the domain classes.

提交回复
热议问题