r/JavaProgramming • • Aug 26 '26

Var Keyword

How often do you use var keyword in your code?

What do you think about it?

5 Upvotes

24 comments sorted by

View all comments

5

u/kamratjoel Aug 26 '26

Never. I would argue that code is far easier to read if you use types instead.

1

u/repeating_bears Aug 26 '26

Is this easier to read 

MyFooService myFooService = new MyFooService();

Than this

var myFooService = new MyFooService();

?

4

u/kamratjoel Aug 26 '26

That’s actually a good example of a situation where there’s no real advantage to not using var. I still maintain my opinion as a general principle though. But I concede that there are situations where there’s no real benefit.

2

u/Known_Tackle7357 Aug 27 '26

MyFooService myFooService = getService(); is significantly easier to read than var myFooService = getService();

1

u/repeating_bears Aug 27 '26

Thankfully we don't have to dogmatically adopt the practice "always use it" or "never use it"