简体   繁体   中英

Where is the proper place to put my own Java code that isn't a Service, Activity, Fragment, etc.?

I read somewhere that all Java code I write should be under the com.my.package (just an example) in the src folder. Is this true? I really don't want to clutter that space with tons of java files (especially the crucial ones like activities, fragments, etc.).

Examples of code I want to place somewhere: A class that takes care of UI Fading, a class that validates emails, etc. I was thinking of putting them under libs , but I was told that is for external libraries not written by me.

just add a new package (ie a sub folder). something like com.my.package.mystuff or com.my.package.utils or something similar. That way you can keep your code separated.

you should be putting them in the main source folder as you said. However, you can have subpackages to avoid things becoming too cluttered. (com.my.package.sub.MyClass). One common pattern I've observed in Google apps is putting activities/fragments in a UI package to keep them clear from the rest of the code.

Make additional packages to organize your files. Typically all of your code should live at least under com.my.package , but not necessarily at that level. For example:

com/my/package
com/my/package/utils
com/my/package/fragment

You can organize it just like any directory structure. There are differences though, since if you have package private fields (eg String x instead of private String x they will not be accessible to classes outside of the same package).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM