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 trialRachelle Wood
15,362 PointsOrder to importing?
Hi all.
I was wondering if there is an order to importing utilities/io in Java other than you import everything first before filling out the class?
4 Answers
James Reynolds
3,325 PointsOk, someone can correct me if I am wrong. But, this is how I see things. Hope it helps!
Yes it is just personal preference. Order of imports doesn't matter. Import basically means "Later in my code when I say 'List' I am referring to this library 'java.util.List'. So ordering does NOT matter.
import java.util.*;
Will also work. The * character basically means "Import all libraries that start with 'import.java.util'. Most compilers will automatically include these libraries. However, it is good practice to import each individual library so when you have an error because of a missing library you will know how to fix this error.
Why does the program still work? ONLY THE CODE IN MAIN IS COMPILED AND EXECUTED LINE-BY-LINE. If you declare a List object within main. It will search for the List class and compile the necessary code.
List myList = new ArrayList();
In English: "Look for object 'List'." "Ok, 'List' was imported earlier with 'import java.util.List;'." "Create an object of type 'List' and call that object 'myList'." Now the List object constructer initiates. "Assign this object a new ArrayList type." The constructor searches for ArrayList and it is found because it was imported earlier in the code. When the compiler finds this object it knows, "Ok, this ArrayList object is a subclass of the object List" Now polymorphism magic happens that I won't discuss.
Alphabetic order is a good practice because someone else working on your code can easily see what libraries you are using. If you ever take algorithms you will learn searching is done quicker with an alphabetized list for both machines and humans.
TL;DR So you see, since the only code that is actually executed line-by-line is within the main. Import order does not matter. It is just a reference of "Hey, this code is here. Just letting you know incase we need it."
James Reynolds
3,325 PointsThere is no specific order for the imported libraries. It is a good habit to list them alphabetically as he has done. These libraries are basically objects or classes similar to what we are creating. So when you declare something like 'Array' the Java compiler tries to match this to a class which is included with 'import java.util.Array'. Then the constructor for this class begins initiation. The compiler doesn't actually compile every line of code. It only executes the code that is necessary or called upon.
Rachelle Wood
15,362 PointsThanks! That is what I was wondering. I was reading a stackoverflow question similar to mine and it looks like it is somewhat based on personal preference?
kabir k
Courses Plus Student 18,036 PointsHi Rachelle Wood,
The order of import is - package declaration, java libraries or package, and then your own package.
In importing the java libraries, it is best practice to arrange them alphabetically. So, that's why ArrayList import comes before the List import and it stills work.
Hope that helps.
kabir
Dane Parchment
Treehouse Moderator 11,077 PointsBasically just follow the package structure, where packages come first, followed by general imports, and then more specific imports. Follow the link below for more details.
http://docs.oracle.com/javase/tutorial/java/package/usepkgs.html
Rachelle Wood
15,362 PointsI read that, but I meant more like in the case of ArrayList and List. In the video, it looks like ArrayList is imported before List yet I thought that ArrayList belonged to List. So why is List not imported before ArrayList? Why does the program still work?
Rachelle Wood
15,362 PointsRachelle Wood
15,362 PointsI did a bit of nosing around and it seems that what you are saying is what I uncovered too. Thanks a lot for a clear and concise explanation!