How to Display Explicit Type Hints for Variables in IntelliJ IDEA

Recently, I installed the latest version of IntelliJ IDEA on my new laptop. When I started writing code, I realized that explicit types for variables weren’t showing up. This can be quite tricky, especially if you’re used to having them. So, here’s a guide on how to enable this feature.

To show explicit types on variables in IntelliJ IDEA, especially when dealing with languages like Java or Kotlin that have type inference (e.g., using var or val), you can configure the IDE to display the inferred type next to the variable. Here’s how to do it:

For Java:

  1. Enable Inferred Types for Local Variables:
    • Go to File > Settings (or IntelliJ IDEA > Preferences on macOS).
    • Navigate to Editor > Inlay Hints > Java > Inferred Types.
    • Enable Show hints for: Local variable types.
    This will show the inferred types next to local variables.

For Kotlin:

  1. Enable Type Hints for Variables:
    • Go to File > Settings (or IntelliJ IDEA > Preferences on macOS).Navigate to Editor > Inlay Hints > Kotlin > Type Hints.Check the option for Show hints for variable types.
    This will show the inferred types next to variables defined using val or var.

As you can see in the image, you just need to check the appropriate checkboxes for Java and Kotlin, and it will work again.

Additional Customization:

  • You can also customize the font size, color, and whether to show type hints for specific elements like lambda expressions or function return types.
  • For further clarity, IntelliJ also allows you to enable or disable type hints for other code elements like function parameters, return types, and more, depending on the language you’re working with.

Once you’ve enabled these settings, IntelliJ IDEA will automatically display the inferred types next to the variables in your code editor, making it easier to understand the types without explicitly declaring them.

Leave a Reply

Your email address will not be published. Required fields are marked *

×