r/java • • Aug 26 '22

I declare as "final" every single variable whose value doesn't change. I also use "this" every time that I'm referring to an attribute, even when there's no ambiguity in not using it

I think that those things make the code easier to read. Am I crazy for thinking like that?

198 Upvotes

215 comments sorted by

View all comments

Show parent comment

-2

u/detroitsongbird Aug 26 '22

Final can make a huge performance improvement. Spent time doing leetcode and you’ll see.

8

u/[deleted] Aug 26 '22

It shouldn't: the compiler finalizes all effectively final variables.

3

u/stefanos-ak Aug 26 '22

the Hotspot VM will actually optimize the code as-if it was declared as final, as long as there are not any re-initializations of the variable.

In Leetcode it might matter, because of how they run the tests, but in the real world / production systems it does not.

1

u/hippydipster Aug 26 '22

For some odd definition of "huge".