August 12, 2024August 12, 2024 How to Configure IntelliJ IDEA to Avoid Wildcard Imports (*) When writing Java code, maintaining clarity and precision in your imports is key to ensuring that your code is clean and easy to understand. Using wildcard imports (*) can obscure which classes are actually being used and make your code less maintainable. To prevent IntelliJ IDEA from automatically using wildcard imports, follow this guide to fine-tune your settings. Why Avoid Wildcard Imports? Before diving into the steps, it’s important to understand why avoiding wildcard imports is considered a best practice: Readability: Explicit imports make it clear which specific classes are being used, making the code easier to read and understand. Avoid Conflicts: Explicit imports help prevent class name conflicts, especially when classes from different packages have the same name. Smaller Scope: Importing only what you need keeps your code’s scope tight, avoiding potential issues with unintended classes being accessible. Step-by-Step Guide to Configure IntelliJ IDEA Follow these steps to configure IntelliJ IDEA to avoid using wildcard imports: 1. Open IntelliJ IDEA Settings Start IntelliJ IDEA. Navigate to the File menu and select Settings (or Preferences on macOS). 2. Navigate to Code Style Settings In the Settings/Preferences dialog, go to Editor > Code Style > Java. Here, you will see a variety of options for configuring how your Java code is formatted. 3. Adjust Import Settings Select the Imports tab within the Java Code Style settings. You’ll see options related to how IntelliJ IDEA handles imports. 4. Set the Class Count to Avoid Wildcards Locate the fields labeled “Class count to use import with ‘*’” and “Names count to use static import with ‘*’”. Change the value of these fields to 999 or any large number. This essentially tells IntelliJ IDEA not to use wildcard imports unless there are an exceptionally large number of imports (which is rare). 5. Apply and Save Your Settings Click Apply and then OK to save your changes. 6. Optimize Existing Imports To apply this setting to your existing codebase, you can use the Optimize Imports feature. Right-click within the editor, go to Code > Optimize Imports or use the shortcut Ctrl + Alt + O (on Windows/Linux) or ⌥⌘O (on macOS). Bonus: Configuring Auto-Imports in IntelliJ IDEA IntelliJ IDEA can automatically manage your imports, but it’s important to ensure that the settings align with your coding standards: Under the Imports tab in the Java Code Style settings, check “Add unambiguous imports on the fly” to automatically import classes as you type. Ensure “Optimize imports on the fly” is enabled to keep your import statements clean and well-organized automatically. Final Thoughts By configuring IntelliJ IDEA to avoid wildcard imports, you ensure that your Java code remains clean, readable, and maintainable. This simple adjustment can have a significant impact on the quality of your code, making it easier to debug, refactor, and understand. So, take a few minutes to tweak these settings in IntelliJ IDEA, and enjoy the benefits of cleaner, more professional code. Read more about SOLID Principle in Java Clean code CleanCodeCodeQualityIDEConfigurationIntelliJIDEAJavaBestPracticesJavaProgrammingJavaTipsprogrammingSoftwareDevelopment