If you use eclipse, try:

Window > Prefferences > Java > Editor > Save Actions

Check "perform the selected actions on save", "Additional Actions" and click "Configure".

Using eclipse's Save Actions can be really useful in real life coding, but you will probably learn some neat java tricks going through the Save Actions wizard.

Java is an Object Oriented language. You need to take advantage of that fact.
Use classes to separate your code into different logical / structural components. Learn how to use OOP. Follow SOLID design and use design patterns.

Another important thing is to know your language. Start by reading basic classes javadocs and relevant sections of the java spec. I would begin with deeply understanding the different types of java (class, interface, enum and inner / nested / anonymous types) and the different modifiers (private, public, protected, static, abstract, final, default).

Some other eclipse's short cuts:

  • CTRL-A, CTRL-I ("indentation") will fix your code indentation.
  • CTRL-SHIFT-O ("organize imports") will omit redundant imports.

  • Answer from Elist on Stack Overflow
    🌐
    Medium
    medium.com › @techisbeautiful › optimizing-your-java-code-structure-best-practices-and-organization-tips-68cdb6e4c647
    Optimizing Your Java Code Structure: Best Practices and Organization Tips | by Tech Is Beautiful | Medium
    March 5, 2023 - The guide covers a range of best practices and tips, including naming conventions, code commenting, package structure, and class organization. By following these tips, you can improve the readability, reusability, and scalability of your code, making it easier to maintain and modify over time. Whether you’re a seasoned Java developer or just starting out, this guide will help you create clean and professional code that meets your project requirements.
    🌐
    Medium
    medium.com › @rbansal8362 › best-practices-for-java-code-organization-and-style-72298c3eab1
    Best Practices for Java Code Organization and Style | by Rohit Bansal | Medium
    June 30, 2023 - Consistent indentation, meaningful naming, well-structured packages and classes, concise methods, proper comments and documentation, effective error handling, and consistent coding style are all important factors in creating high-quality Java code.
    Discussions

    Are there best practices for (Java) package organization? - Stack Overflow
    Further to that, I have found that ... well in practice as it has many advantages of 'package by feature': Promotes creation of reusable frameworks (libraries with both model and UI aspects) Allows plug and play layer implementations - virtually impossible with 'package by feature', because it places layer implementations in the same package/directory as the model code. Many more... I explain in depth here: Java Package Name Structure and Organization, but my standard ... More on stackoverflow.com
    🌐 stackoverflow.com
    recommendation for java code organization

    Use interfaces for everything you can and avoid abstract base classes. Then, when you come across an issue where it feels like it may be easier to use ABCs, first consider if your design is right or whether it can be organised differently. If you conclude that it still makes sense, then and only then use abstract base classes.

    Interface-based development forces you to keep implementation detail out of your contracts much more than ABCs, and supports multiple inheritance. ABCs are far less flexible, do not work as nicely for composition and delegation, and can result in more messy code down the line if you don't get it right.

    ABCs are much more tightly coupled to your implementation than interfaces are. Interfaces also have the benefits of multiple inheritance as already stated in the previous paragraph, and can be used as functional interface types in some cases. They can also be used with enums, records, and other interfaces as a superinterface. Much easier to test too, as your unit tests don't have to argue with implementation details you inherit as much (this would be a code smell, but far easier to avoid if you avoid them in the first place).

    Remember your code should define what it does as part of the API for other components to use, not how it does it.

    More on reddit.com
    🌐 r/learnjava
    6
    9
    February 10, 2024
    java - What is the best way to organize object oriented code? - Stack Overflow
    I haven't coded in java for a long time, and after coding in C, I'm having issued organizing my code for OOP. More specifically I'm not sure when to create a new method, and when to create a new cl... More on stackoverflow.com
    🌐 stackoverflow.com
    java - How to keep code clean and organized? - Stack Overflow
    Keeping a code clean and organized is important for future maintenance, specially for companies where programmers cycle (or get fired, hopefully not). I have been looking around on Google and ive... More on stackoverflow.com
    🌐 stackoverflow.com
    Top answer
    1 of 3
    3

    If you use eclipse, try:

    Window > Prefferences > Java > Editor > Save Actions

    Check "perform the selected actions on save", "Additional Actions" and click "Configure".

    Using eclipse's Save Actions can be really useful in real life coding, but you will probably learn some neat java tricks going through the Save Actions wizard.

    Java is an Object Oriented language. You need to take advantage of that fact.
    Use classes to separate your code into different logical / structural components. Learn how to use OOP. Follow SOLID design and use design patterns.

    Another important thing is to know your language. Start by reading basic classes javadocs and relevant sections of the java spec. I would begin with deeply understanding the different types of java (class, interface, enum and inner / nested / anonymous types) and the different modifiers (private, public, protected, static, abstract, final, default).

    Some other eclipse's short cuts:

  • CTRL-A, CTRL-I ("indentation") will fix your code indentation.
  • CTRL-SHIFT-O ("organize imports") will omit redundant imports.

  • 2 of 3
    2

    You might consider taking a look at Code Complete, which deals with the issues that you're concerned with here, and otherwise is just a classic in our field that every serious developer should read.

    In general, when you're organizing code you should do so with a few things in mind: readability and atomicity. These two factors apply to code on every level of an application, from variable naming, routines, methods, classes, packages, and so on.

    Readability is a simple idea: can a human being read this code and understand it? To gauge the readability of the code all you have to do is read it! Do variable names help the reader understand what something is? Are routines and classes properly formatted and not needlessly complex? Have you removed all code that isn't being used? Is your code written in a logical progression?

    Atomicity is the idea that everything should have one purpose. A function or method should (usually) do one thing and one thing only. A class should usually be a logical grouping of related methods and fields serving some type of unique purpose, and NOT a mish-mash of unrelated stuff. A package should also contain a set of related files. Same with a project, and so on.

    The main benefit of atomicity is that once you get into more involved applications it's actually much easier to debug and isolate issues in your code because you know where stuff is. For instance: I have a database access error! Good thing I have a package that's specifically defined for my database access objects.

    I know when I was just getting started in the field this was something that threw me off too. It might not be until you do a lot of coding within more significant apps that you really start to understand best practices and why people build stuff a certain way.

    🌐
    Medium
    medium.com › @msandin › strategies-for-organizing-code-2c9d690b6f33
    Four Strategies for Organizing Code | by Martin Sandin | Medium
    April 27, 2016 - This article outlines four different strategies for organizing code: by component, by toolbox, by layer, and by kind. I think these four…
    Top answer
    1 of 7
    225

    I organize packages by feature, not by patterns or implementation roles. I think packages like:

    • beans
    • factories
    • collections

    are wrong.

    I prefer, for example:

    • orders
    • store
    • reports

    so I can hide implementation details through package visibility. Factory of orders should be in the orders package so details about how to create an order are hidden.

    2 of 7
    200

    Package organization or package structuring is usually a heated discussion. Below are some simple guidelines for package naming and structuring:

    • Follow Java package naming conventions
    • Structure your packages according to their functional role as well as their business role
      • Break down your packages according to their functionality or modules. e.g. com.company.product.modulea
      • Further break down could be based on layers in your software. But don't go overboard if you have only few classes in the package, then it makes sense to have everything in the package. e.g. com.company.product.module.web or com.company.product.module.util etc.
      • Avoid going overboard with structuring, IMO avoid separate packaging for exceptions, factories, etc. unless there's a pressing need.
    • If your project is small, keep it simple with few packages. e.g. com.company.product.model and com.company.product.util, etc.
    • Take a look at some of the popular open source projects out there on Apache projects. See how they use structuring, for various sized projects.
    • Also consider build and distribution when naming (allowing you to distribute your API or SDK in a different package, see the servlet API)

    After a few experiments and trials, you should be able to come up with a structuring that you are comfortable with. Don't be fixated on one convention, be open to changes.

    🌐
    Finoit Technologies
    finoit.com › articles › java-coding-standards-best-practices
    Top 5 Java coding standards and best practices in 2026
    December 10, 2025 - Most importantly, you must always ensure to write readable codes easily understood by humans (and not just write codes to satisfy the compiler). It is a standard Java coding practice used by millions of developers. Another critical coding practice in Java you should always attempt to implement when writing is organizing member variables of a class through their scopes.
    🌐
    CodeJava
    codejava.net › coding › 10-java-core-best-practices-every-java-programmer-should-know
    10 Java Core Best Practices Every Java Programmer Should Know
    June 29, 2019 - So do not write code just to satisfy ... other guys who end up maintaining your project. The best practice to organize member variables of a class by their scopes from most restrictive to least restrictive....
    Find elsewhere
    🌐
    Quora
    quora.com › What-are-the-best-practices-to-organize-code-into-a-java-project-files-subfiles-classes-packages-etc
    What are the best practices to organize code into a java project (files, subfiles, classes, packages etc.)? - Quora
    Answer (1 of 2): Just follow the Maven standard here: Introduction to the Standard Directory Layout Maven, the wildly popular build/dependency management tool, expects files to be in certain places. Other tools, such as sbt, followed this convention. It's a good layout, so why fight against so...
    🌐
    Codepath
    guides.codepath.org › android › Organizing-your-Source-Files
    Organizing your Source Files | Android Development | CodePath Guides
    The following naming and casing conventions are important for your Java code: See this naming guide for more details. Android classes should be named with a particular convention that makes their purpose clear in the name. For example all activities should end with Activity as in MoviesActivity. The following are the most important naming conventions: Use your best judgement for other types of files. The goal is for any Android-specific classes to be identifiable by the suffix. There are several best practices for organizing your app’s package structure.
    🌐
    Oracle
    oracle.com › java › technologies › java se
    Code Conventions for the Java Programming Language: 3. File Organization
    Each Java source file contains a single public class or interface. When private classes and interfaces are associated with a public class, you can put them in the same source file as the public class.
    🌐
    Baeldung
    baeldung.com › home › architecture › clean coding in java
    Clean Coding in Java | Baeldung
    March 18, 2026 - While Java doesn’t enforce any project structure, it’s always useful to follow a consistent pattern to organize our source files, tests, configurations, data, and other code artifacts.
    🌐
    Medium
    medium.com › @zknill › how-to-structure-a-java-codebase-bcbb42f40f86
    How to structure a Java codebase. Best practice for Java codebases | by Zak Knill | Medium
    December 5, 2022 - How to structure a Java codebase Best practice for Java codebases, use domain driven design The short answer is “by domain”. When you build a software application — no matter if it’s a web …
    🌐
    Reddit
    reddit.com › r/learnjava › recommendation for java code organization
    r/learnjava on Reddit: recommendation for java code organization
    February 10, 2024 -

    Hi everyone! I am new to java and have some questions about code organization. My learning process is going smoothly so far as the syntax goes, but i have hard time grasping organizational stuff, such as where would i use interfaces vs abstract classes, especially when interfaces have default methods. I have found a lot of theory about this, but less practical examples. I would also like to get good at architectural patterns in practice. What would the best approach to do that? When I am coding, I notice that the codebase is going out of control really fast. thanks for any input and suggestions.

    🌐
    CLIMB
    climbtheladder.com › 10-java-project-structure-best-practices
    10 Java Project Structure Best Practices - CLIMB
    July 16, 2025 - Additionally, using relative paths makes it easier for other developers to understand your code since they don’t have to guess where a file is located. Organizing source files by package helps keep your code organized and easy to find.
    🌐
    Reintech
    reintech.io › blog › java-project-structure-organizing-managing-large-projects
    Java project structure: Organizing and managing large projects | Reintech media
    January 29, 2026 - Organizing and managing large Java projects can be challenging, but using a standard project structure, organizing your code into packages and layers, leveraging dependency injection, and maintaining a test-driven development mindset can make ...
    🌐
    Theskilledcoder
    theskilledcoder.com › p › 14-powerful-java-code-organization
    14 Powerful Java Code Organization Strategies - Skilled Coder
    March 14, 2025 - Start with simpler structures for smaller projects, and adopt more sophisticated patterns only when they solve actual problems your team is facing. The best structure is one that makes your codebase more maintainable and your development process ...
    🌐
    Digma
    digma.ai › clean-code-java
    How to write clean Java code: Best practices
    February 25, 2024 - What is clean Java code? Practices for writing clean Java code that promotes expressiveness, conciseness, organization, and maintainability.
    🌐
    NV5 Geospatial Software
    nv5geospatialsoftware.com › docs › OrganizeCodeBestPractices.html
    Best Practices in Organizing Your Code
    View our Documentation Center document now and explore other helpful examples for using IDL, ENVI and other products.
    Top answer
    1 of 2
    4

    Where to start

    I'm pretty sure you don't apply all best practices if you have many difficult to understand fragments similar to:

    if inside if inside for

    A very good place to start with clean code is reading a book related to the topic. Although books are sometimes criticized, it allows to cover many aspects at once and make the knowledge possessed on tutorials, blog notes, SO more systematic. Personally, I can recommend Clean Code by Robert C. Martin.

    If you are familiar with a theory, practice a lot. Write a code and improve it. Again and again. It is also useful to find somebody, who is more experienced and ask him about more difficult cases.

    I suggested below three points that are very important from my point of view.

    Self-documenting code

    First of all, I would reduce the amount of comments to minimum, avoid dashed lines, "editor-folds" and so one. If you need to split your code with separators - the class is probably too long. Please, read more about good comments and self-documenting code here.

    Unconditional programming

    Many times it is a good idea to replace if-else and switch instructions with some OOP principles. This answer contains a couple of useful links.

    Package/Class/Method size

    Reorganize all of the routines, that are too big to easily understand. Split long methods into smaller, redesign God classes using OOP paradigms. The rule applies also to projects - you can split it into modules or subprojects and reuse. Single responsibility principle might be helpful - some examples.

    2 of 2
    1

    A good code is pretty much its own documentation.

    Java documentation provides lots of details on how to write code in a better way.

    My suggestions are:

    Use proper naming conventions:

    In your code i can see some code smells, the variables test and test2 can be the big cause of trouble for other programmers.

    Try making modules:

    Making modules can also make code easy to understand. For example creating possible methods out of long code and naming them properly. My teacher once told me: "if you cannot name a method or think of any appropriate name for your method it means you are doing it wrong"

    Comments are thought to be important but my opinion is good written code do not rely on comments. You can add them to explain what is complex.

    Proper Indentation is also important.