Pure Danger Tech


navigation
home

Java 7 short declarations – why bother?

24 Jan 2007

Yeesh. I just read Stephen Colebourne’s post on Java 7 short declarations regarding reducing the amount of typing required in declaring variables like Foo f = new Foo();.

My first thought is that this is definitely on the low importance end of reasons to change the language. Generics have made the variable declaration quite a bit messier, especially in the case of nested generics that need to be defined on both LHS and RHS of the assignment. But that’s only a smallish portion of the new assignments you make in your code. The vast majority of the time this is a non-issue, especially when you consider the help the IDE can give you in this area.

I think the argument about the precious “Java-Style” is bunk. It certainly wouldn’t bother me to not specify the type on the LHS and I really can’t imagine too many Java programmers I know being morally offended by this horrifying :) violation of Java-Style.

Option A seems like the simplest but why do we even need a special operator. Why not just: foo = new Foo(); From a usage point of view, this looks fully declared and unambiguous to me. If the type isn’t specified on the LHS then it is just the type created on the RHS. Seems much simpler to me.

I don’t see why we would want to restrict this to final in option B or introduce yet another keyword in option C. D is just silly for non-generics (the most common case). I think all the E options are more confusing.

Of course, I think my first vote would be to leave well enough alone. If you want to do something truly useful, I’d rather have inferred types in many places (not just new object construction). I’m sure that’s a much bigger deal but it would also be far more useful.