How to show concrete type instead of var in variable declaration

已回答

I have read a hint somewhere how to configure the Java editor to show concrete types instead of var like this:

Class myClass = new Class(); instead of var myClass = new Class();

and not as inlay hint. There is also a special highlighting for this. have configured this in a preview installation of IntellJ. Today i deinstalled the installation because the new version was released and i didn't saved the configuration. Now i can't find the hint anymore and also can't remember which configuration option i changed. Can anyone tell me how to configure this?

Kind Regards

Andreas

 

0

Hello, please see the following option: Preferences | Editor | General | Code Folding | Replace var with inferred type. Thank you 

0

Hi Olga,

thanks for the help. It's not quite obvious that the option is placed under "Code folding". I looked at "Code Completion", "Code Editing" and "General".  I didn't think about searching for "inferred type" 😒, but i searched for "replace var" (without hyphen) and nothing was found. Maybe you could improve this a little bit. I find this option extreme helpful, specially because our Sonarqube rules enforces using var instead of concrete types.

Kind Regards

Andreas

0

Hi Andreas,

Please clarify what version of IntelliJ IDEA do you use? The following search works for me:

0

until yesterday i used 2021.1.3. I just upgraded to 2021.2 and now it works.

0

Great! Thank you for confirming it works👍

0

I'm using Intellij IDEA 2023.3.2 Ultimate and I have enabled the “Replace ‘var’ with inferred type” option for Java. However, when I use Ctrl-Alt-V to create a variable it still comes up with "final var". Is there some other option I have to enable?

0

Wvanalbada please check if you have “Use ‘var’ for local variables declaration” enabled in “Settings | Editor | Code Style | Java | Code Generation”

0

Thank you Sergei! That was the problem indeed.

0

Hi Olga Klisho 

could you please advice how I can create variable with var in one place and with exact type in another place, any shortcuts?

because the setting mentioned above works for all 'create variable actions' - how can I say what I need for each case - when I create a variable?

--

IntelliJ IDEA 2024.1.1 (Ultimate Edition)

0

Hello,

You may try using the “Replace ‘var’ with explicit type” intention:

 

1

IntelliJ IDEA 2024.2.0.2 (Ultimate Edition), Windows 10
When I search “replace var”,  “Code folding” still not show
 

0

Hi 1120718914  Can you share a screenshot of page File | Settings | Editor | General | Code Folding ? It shuld be the same as https://intellij-support.jetbrains.com/hc/en-us/community/posts/4404384514834/comments/4404424975250. I'm using IDEA 2024.2.1.

0

Jacky Liu I have already upgrade to 2024.2.1, it works. So I cannot show you a picture. And yes, in 2024.2.0.2,  “code folding” not show when search, but “code folding” absolutely is there.

0
Thanks for the update! I'm glad to hear that it works now.
0

请先登录再写评论。