Wildcard imports…not as bad anymore?
May 29th, 2004 by HenI’ve always been very against ‘import java.util.*;’ and its ilk, for 2 very good reasons.
Firstly, there is the classic clash possibility where java.util.Date and java.sql.Date hit each other and you have no idea which ‘new Date(42L)’ matches. Secondly, there is the readability problem where a developer new to the code has no idea which package a class came from.
While the latter is not a huge problem with the JDK classes, it is a pain in the arse with someone else’s code.
As a part of teaching a class, I had cause to try out the classic Date clash. In fact, I used ‘new Date()’, which can only hit java.util.Date as java.sql.Date has (long) and (int, int, int) as contructors. I got back the following compiler error:
reference to Date is ambiguous, both class java.util.Date in java.util and class java.sql.Date in java.sql match
I’m unsure when this turned up, but am pretty sure JDK 1.1 did not have it.
So my first reason no longer applies at all. The only reason to avoid a wildcard import (and I don’t mean the evil new JDK 1.5 ones) is to make code more readable.
As standard J2SE/J2EE classes should be known, I’m ready to start using ‘import java.util.*’.
(400th blog entry)
