Is using @var a bad practice in Java?

1,611 views
Skip to first unread message

CroaBeast

unread,
Jun 24, 2023, 10:51:50 AM6/24/23
to Project Lombok
Been a Java programmer for almost a year and didn't know about the @var annotation until two months ago.

I was using it in some projects but a couple of friends told me that using @var is a bad practice because "java is made to be strongly typed not weakly typed", "using val and var makes for confusing code too" and "bad in terms of active public projects".

I don't quite understand why @var can be bad. So I want to ask that here.

Is that bad or not?

Victor Williams Stafusa da Silva

unread,
Jun 24, 2023, 1:56:29 PM6/24/23
to project...@googlegroups.com
It is not bad, just not needed anymore. But was not removed for the sake of compatibility I guess.

Since Java 10, we have the native "var" keyword, and it does almost exactly the same thing as Lombok's "var". Using the "var" keyword in Java is not a bad practice.

And although certainly there are some contrived cases where using "var" is bad, the same could also be said about any other feature of the language.

Lombok also features "val", which is the immutable version of "var". People of Oracle/OpenJDK/etc also considered that, but they felt unnecessary and considered that "final var" was already good enough.

And except if you are targeting Java <= 9, it is just better to use the regular keyword instead.

--
You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombo...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/project-lombok/f9c30978-4ca2-42e8-bcb6-bc29bc7bb850n%40googlegroups.com.

Mat Jaggard

unread,
Jun 24, 2023, 4:59:43 PM6/24/23
to project-lombok
It's also worth noting that none of these stop Java being strongly typed. The type of each variable is defined at declaration time and cannot be changed.

Floris Kraak

unread,
Jun 25, 2023, 3:34:52 PM6/25/23
to project...@googlegroups.com
The best practice that I've seen basically comes down to "if the type is obvious from the code, use var. Otherwise, use explicit types".

Whenever you new something, you the new statement already makes it obvious. Whenever you use certain static factories, such as 'List.of("foo")', it can make sense as well.

There is some subjectity involved but it's a good rule.

Adwait Kumar Singh

unread,
Jun 25, 2023, 8:05:13 PM6/25/23
to project...@googlegroups.com
There is also a style guide for Local Variable Type Inference from OpenJDK, https://openjdk.org/projects/amber/guides/lvti-style-guide

Thiago Milczarek Sayão

unread,
Jun 27, 2023, 7:03:50 AM6/27/23
to project...@googlegroups.com
var was introduced as a java feature since java 10. The compiler will replace with the correct type, so there's still a type, nothing changed.

So, there's no point in using @var unless using java 9 or below.

I personally don't use it very much because typing the type before will lead to IDE code completion.



--
Reply all
Reply to author
Forward
0 new messages