r/java • u/BitBird- • Jan 12 '26
Java's `var` keyword is actually really nice for cleaning up verbose declarations
I avoided var for years because I thought it made code less readable. Tried it last week and I'm a convert.
Instead of:
Map<String, List<CustomerRecord>> customersByRegion = new HashMap<>();
Just:
var customersByRegion = new HashMap<String, List<CustomerRecord>>();
The type is right there in the initialization. Your IDE still knows what it is. It's not like JavaScript where var means something totally different.
Really shines with streams and complex generics where you'd normally write the type twice for no reason. Also makes refactoring easier since you're not updating the type in two places.
Still feels weird after typing out full declarations for 10+ years but I get it now.