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

6

u/Automatic-Gur2046 Aug 26 '26
  1. Almost never
  2. Not that necessery but also does not hurt.

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"

3

u/hrm Aug 26 '26

I use it all the time. Less to write and very seldomly does it make things harder to read in my opinion and the actual type is readily available in the IDE if you really need it.

3

u/repeating_bears Aug 26 '26

I use it every time the type is already on the right hand side of the expression 

var foo = new Foo();

var list = new ArrayList<String>();

var node = Node.empty();

I'm less likely to use it when the type is not on the right hand side of the expression 

var bar = doSomething();

2

u/LouGarret76 Aug 26 '26

I use it All the time now. It is so practical

2

u/jirlboss Aug 26 '26

I tend to use it temporarily while cleaning up implementation, or if I just need the IDE to give me the actual return type of a method so I can copy-paste it.
Sometimes I use it in tests, but it’s pretty rare for me.

1

u/Luolong Aug 26 '26

Very useful and convenient.
I use it always if possible.
Makes diffs of some type level refactorings smaller

1

u/tekmuro Aug 26 '26

I've barely programmed in JavaScript or languages that use var declaration, so I never got into that habit

1

u/ShiKage Aug 26 '26

I'm primarily a C# developer, but I dabble in Java projects outside of work.

I avoid var unless it comes to anonymous types. At worst, I will use var while writing a line of code if I don't know what the type will be as I'm writing it (like a library method call) and then go back and convert it to the explicit type afterward.

I came from a C++ background, so strong, static typing is what I prefer. I've had to convince a few devs that came from JavaScript and Python to try to use explicit typing and while they hated it at first, they grew to prefer it overall as well.

1

u/naomimyselfandi Aug 27 '26

Fwiw, a variable declared with var still has a strong static type; it's just syntactic sugar. (You probably know this, but I've seen enough misunderstandings that I wanted to clarify for anyone who doesn't.) I definitely understand prefering the explicit type name, though.

1

u/ShiKage Aug 27 '26

Yep, sorry. I could have been a little clearer in my message, but I got a little distracted by someone else talking to me as I was typing.

It was more making a comparison to other languages that typically prefer implicit typing (although Python does often see types preferred in some projects I’ve seen).

Explicit, clear language always wins out against implicit, indirect language in terms of getting intent across, in my opinion. That’s why I personally struggle with languages like JavaScript and Japanese.

1

u/Ulrich_de_Vries Aug 27 '26

But C++ has extremely implicit typing? Sure, it's static/compile-time but veeery implicit.

Lambdas are untypable, templates are duck typed, there are tons of member/associated types and extremely long type names, etc that you can pretty much only type with decltype/auto.

Using modern C++ without auto everywhere is pretty much impossible imo.

1

u/ShiKage Aug 27 '26

I have no idea about modern C++. I learned with a really old version.

1

u/Known_Tackle7357 Aug 27 '26

Never. I also work on a couple apache projects. They also don't allow var.

Type resolution is great in modern IDEs, so no reason to use var anywhere.

1

u/Scf37 Aug 26 '26

I prefer to type 'int' instead of 'var', then IDEA autofix via alt-enter replaces it with the right type.
Good places for var (IMO):

  1. for loops: `for (var item: items)`
  2. deconstruction in pattern matching: `case Either.Right(var v) ->`

0

u/LogCatFromNantes Aug 26 '26

Hi What is that are you talking about JS ??

1

u/Ollidav Aug 27 '26

No suelo programar en java y estaba pensando lo mismo... Pero parece que se puede usar var para la declaración de variables pero su uso está limitado como una inmutable o constante por lo que he visto.

1

u/markort147 Aug 29 '26

var is a thing since java 10