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!
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
Oliver Acevedo4,338 Points
What does organizing imports actually do? Is that the same as "Optimizing Imports"? I don't see any difference.
I hear Ben saying, "Let's organize our imports" all the time and have no idea what he's talking about. Ctrl + O doesn't do what he says it does. It "Overrides Methods". I do ctrl+shift+O and that seems to be to Optimize Imports but still I have no idea what it's doing and why we need to do it all the time.
This is much related to core concept of Java. To increase efficiency of a program, not all library declarations that are available are used at once. Only those that are needed are imported into our program.
For instance the Random java function which you might have used in the earlier app tutorial. Random is a class already built inside java that has the functionality to produce random numbers. By default Java, thus Android has no clue what Random means. Only when we import java.util.Random library, that it gets a clue.
Another benefit of importing is that you don't have to use the class extension everywhere. Instead of importing Random we could have written
java.util.Random random = new java.util.Random();
This is perfectly valid in java. But imagine if we had to use Random 100 times inside the program. So when we import java.util.Random we simply use the name Random instead of the complete extension.
You may read more about packages and imports here Oracle Knowledge base
I've been following Ben's video's too, but i don't remember him saying just Ctrl+O, its in fact Ctrl + Shift + O or Cmd + Shift+ O in Mac
When we say organize / optimize imports it simply means, explicitly import those library files into our program that have not been set yet
I hope this helps.