Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Organizing Data Arrays

The use of * when importing

Mr.Craig imported the classes Arrays and Date separately, as follows: import java.util.Date; import java.util.Arrays;

But he could simply imported them both just like this: import java.util.*;

Is it a bad practice to do so ? Is there any reason for not using the * ? Or he just did not use it to explain to us in details?

3 Answers

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

Probably to show you so you know to include these imports. Once you know them you can simplify it. But its also easier to debug when you can see exactly whats being imported. You may also import other stuff you may not need & it may slow loading time.

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I'm inclined to agree with Cindy Lea. I think there may be far more to this library than you realize and loading the entire thing when you only need two things is definitely overkill. Here's what oracle has to say about this library. Take a look at what all is in there :smiley:

https://docs.oracle.com/javase/7/docs/api/java/util/package-summary.html

Simon Coates
Simon Coates
28,694 Points

I had always thought there was some kind of speed decrease (this might be limited to compile). But on consideration, the classes you get inadvertently might represent name collisions. I'd be concerned that you might want to use a class that you should import, but accidentally have access to a class with the same name (and possibly a confusingly similar public interface). For example, even when just using java's standard libraries, I think there are multiple date classes. I think this might be what Cindy Lea meant by ease of debugging (?). And as Jennifer Nordell states, you're probably not going to be aware what's in a package, particularly one that's in a non-standard or exotic library. (my knowledge of java is only a passing familiarity, hence i apologise if this is a little naive.) Update: i had thought it was bad practice, but i've just been reading up a little at forums (see here and here) and there are a lot of people who recommend use of *. One of the people quoted Clean Code by Robert C Martin.

"Wildcard imports can sometimes cause name conflicts and ambiguities. Two classes with the same name, but in different packages, will need to be specifically imported, or at least specifically qualified when used. This can be a nuisance but is rare enough that using wildcard imports is still generally better than specific imports."